-
Need your opinion
Which code is more efficient, the first one or the second one?
1) this code draws, "one by one", an ASCII graphical element on the screen
Code:
short i = 0;
for (i = 0; i < 60; i++)
{
printf ("%c", 196);
}
2) this code draws, "everything at once", an ASCII graphical element on the screen
Code:
char Lines[65];
short i = 0;
for (i = 0; i < 60; i++)
{
Lines[i] = 196;
}
Lines[i] = '\0';
printf ("%s", Lines);
-
I would think the second one is better because there is only one printf function call. The first one has to call the function 60 times. Every time you call a function there is a small bit of overhead involved in pushing all the function parameters onto the stack and so forth. Calling a function once is therefore better than 60 times.
-
The second one may be more efficient, but the first one is more readable, so I'd go with it. Any more opinions?
-
Here's a better method:
char lines[65] = {0};
memset( lines, 196, 60 );
printf( "%s", lines );
Efficient and readable.
Quzah.
-
That is really a better method. Thanks quzah.