help, whats wrong? (newbie)

Started by denise_amiga, January 06, 2007, 02:09:28 AM

Previous topic - Next topic

denise_amiga

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.


#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;
}

Pelle

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:


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

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

Pelle

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

Robert

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