NO

Author Topic: help, whats wrong? (newbie)  (Read 3613 times)

denise_amiga

  • Guest
help, whats wrong? (newbie)
« on: January 06, 2007, 02:09:28 AM »
i am new in C, in the next sample code, in my machine,  with n=10000 run in 250 ms. this is "ok".
but with n=100000 the time is 25600, why?
in the asm version, the time is linear. with n=1000000 -> 17 ms.
and with n=10000000 -> 172 ms.

Code: [Select]

#include <windows.h>
#include <string.h>
#include <stdlib.h>

int szadd(char *dest, char *source, int pos)
{
int temp;
__asm
{
mov eax, -1
mov edi, source
mov esi, dest
add esi, pos

 lb1:
add eax, 1
movzx ebx, BYTE PTR[edi +eax]
mov[esi +eax], bl
test bl, bl
jnz lb1

add eax, pos
mov temp, eax
}
return temp;
}

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
long n, t, p;
char *buffer;
char txt[100] = "test ";

buffer = malloc(1024 * 1024 * 64);

p = 0;
n = 10000; //100000 overtime
t = GetTickCount();
while (n > 0)
{
//      p=szadd(buffer,txt,p);
strcat(buffer, txt);
n--;
}
t = GetTickCount() - t;
wsprintf(txt, "time: %d\n", t);
MessageBox(0, txt, "", 0);
free(buffer);
return 0;
}

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: help, whats wrong? (newbie)
« Reply #1 on: January 06, 2007, 08:20:10 AM »
You are comparing apples and oranges. The strcat() function don't have a 'current position', so it must scan for the terminating nul character (in buffer) on each call, before it can append the new string at that position. As the string length increases, so will the time to find the terminator.

Try using a pointer. Change the code to something like this:

Code: [Select]

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
long n, t, p;
char *buffer;
char *ptr;  /*NEW*/
char txt[100] = "test ";

buffer = malloc(1024 * 1024 * 64);
ptr = buffer;  /*NEW*/

p = 0;
n = 10000; //100000 overtime
t = GetTickCount();
while (n > 0)
{
//      p=szadd(buffer,txt,p);
//      strcat(buffer, txt);
ptr += strlen(strcpy(ptr, txt));  /*NEW*/
n--;
}
t = GetTickCount() - t;
wsprintf(txt, "time: %d\n", t);
MessageBox(0, txt, "", 0);
free(buffer);
return 0;
}
/Pelle

denise_amiga

  • Guest
help, whats wrong? (newbie)
« Reply #2 on: January 06, 2007, 11:30:54 AM »
ok pelle.
that is what I looked for, and  is the literal translation of the version in asm, now I must learn much C.
another question, you will know some good documentation of libraries of c, with examples to continue learning.
thank you very much, your work seems to me fantastic, your compiler is brilliant.

pd. sorry for my english

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
help, whats wrong? (newbie)
« Reply #3 on: January 06, 2007, 09:49:46 PM »
Quote from: "denise_amiga"
another question, you will know some good documentation of libraries of c, with examples to continue learning.


Not sure I'm up-to-date...

WCRT is from the link page (http://www.smorgasbordet.com/pellesc/links.htm), and is a "small Win32 based C runtime library": http://wcrt.sourceforge.net/

It's not complete, but perhaps interesting to look at...?
/Pelle

Offline Robert

  • Member
  • *
  • Posts: 245
help, whats wrong? (newbie)
« Reply #4 on: January 06, 2007, 10:23:03 PM »
Quote from: "denise_amiga"
another question, you will know some good documentation of libraries of c, with examples to continue learning.


Other than the the Pelle's C Helpfile, my favourite C reference is the Microsoft Run-Time Library Reference. Most of the function descriptions include examples.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/vcrefruntimelibraryreference.asp

Robert Wishlaw