Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Which is faster?

Reply
Thread Tools

Which is faster?

 
 
Justin Robbs
Guest
Posts: n/a
 
      04-21-2004
I need to fill a number of character fields with spaces or zero's
depending on the field. Which method is faster.

char var[10] = " ";
var[9] = '\0';

or

char var[10];
sprintf( var, " " );

or

char var[10];
strcpy( var, " " );
var[9] = '\0';

or

int i;
char var[10];
for(i=0; i<10; i++)
{
var[i] = ' ';
}
var[i] = '\0';

Thanks for the help.

Justin


 
Reply With Quote
 
 
 
 
Ahmed S. Badran
Guest
Posts: n/a
 
      04-21-2004
Justin Robbs wrote:
> I need to fill a number of character fields with spaces or zero's
> depending on the field. Which method is faster.
>
> char var[10] = " ";
> var[9] = '\0';

I would think this one is the fastest, will probably be translated by the
compiler by a 'multiple store' instruction.
> or
>
> char var[10];
> sprintf( var, " " );

Involves a function call, pushing to the stack, changing program counter,
going back and forth.
> or
>
> char var[10];
> strcpy( var, " " );
> var[9] = '\0';

same as above
> or
>
> int i;
> char var[10];
> for(i=0; i<10; i++)
> {
> var[i] = ' ';
> }
> var[i] = '\0';

Faster than the function calls, the entire code fragment will be probably
held in the processor cache, I would say this one could be comparable to the
first. This way you will have lots of 'very fast executing' instructions
with minimal processor-memory transactions (of course not taking into
account the memory writeback from the cache), compared to probably a single
'multiple store' in the first case. However, I personally think the laster
would be the fastest
> Thanks for the help.


Think about that too;

int i =0;
char tmp= ' ';
while (i < 10) {
var[i++] = tmp;
}

I would think this would be the fastest, everything will be cached on the
processors cache, including the variables.

Ahmed



 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      04-21-2004
Justin Robbs <> spoke thus:

> I need to fill a number of character fields with spaces or zero's
> depending on the field. Which method is faster.


How about

char var[10];
memset( var, ' ', sizeof var - 1 );
var[9]=0;

? I don't recommend that method, however

> char var[10] = " ";
> var[9] = '\0';


I'd say this is your best bet for speed, since all the work can be
accomplished at compile time.

> char var[10];
> sprintf( var, " " );


> char var[10];
> strcpy( var, " " );
> var[9] = '\0';


strcpy appends a null terminator (assuming the destination string is
large enough), so you don't need to do that explicitly.

> int i;
> char var[10];
> for(i=0; i<10; i++)
> {
> var[i] = ' ';
> }
> var[i] = '\0';


This is probably slowest, since the library routines may be able to
use special implementation features to speed up the operation. I
could be wrong on that, however.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
John Cochran
Guest
Posts: n/a
 
      04-21-2004
In article <c6662m$rs8$>,
Justin Robbs <> wrote:
>I need to fill a number of character fields with spaces or zero's
>depending on the field. Which method is faster.


Benchmark the various ways on your system and find out for yourself.
Also, is speed really that much of an issue?

But, here are some comments on each of your methods.

>char var[10] = " ";
>var[9] = '\0';


This method will only work at the time you declare the variable. Attempting
the following code
var = " ";
will not work. Given that limitation, the assignment
var[9] = '\0';
is useless because the string is already NUL terminated.

>
>or
>
>char var[10];
>sprintf( var, " " );

I suspect that this is the worst of the lot since printf and family have
a far amount of overhead.


>
>or
>
>char var[10];
>strcpy( var, " " );
>var[9] = '\0';

I suspect that this would be towards the top of the list in speed, but
that var[9] = '\0'; is useless. Get rid of it.

>
>or
>
>int i;
>char var[10];
>for(i=0; i<10; i++)
>{
> var[i] = ' ';
>}
>var[i] = '\0';


Ick. This one doesn't do what you expect. It contains a buffer overflow.
But if you fix it, it's performance may be comparable to using strcpy().
The only question is the overhead of a function call to strcpy() and if
the vender supplied library uses machine specific tricks to gain speed.

But, I'll repeat an old saying about programming.

Premature optimization is the root of all evil.
Tony Hoare, restated by Donald Knuth.

Think carefully about if this assignment to a string really matters in
terms of performance. It most likely doesn't. Pick good algorythms for
your code and then make the implimentation of the good algorythms as clear
as possible. As a simple example, imagine the relative performance of
2 sort algorythms.

Program one.
Uses a bubble sort as its primary algorythm and is optimized as much
as possible.

Program two.
Uses a heap sort as its primary algorythm and not a lot of attention is
made to reduce CPU cycles.

For very small amounts of data, program one may be faster, but for larger
amounts of data, program two will be so much faster than program one, it
isn't even funny. An O(N log N) algorythm vs an O(N^2) algorythm totaly
swamps any effect optimization may have.

Use the strcpy() and simply make your program readable.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-21-2004
Justin Robbs wrote:
>
> I need to fill a number of character fields with spaces or zero's
> depending on the field. Which method is faster.


Answer #1: It's impossible to say. The C language
is implemented on many platforms with different performance
characteristics. Method X may be faster than Method Y on
Platform A, but slower on Platform B, and run at identical
speed on Platform C.

Answer #2: It is surpassingly unlikely that the speed
of such a trivial operation will have any detectable effect
on the speed of your program. You are almost certainly
optimizing the wrong thing; an old friend of mine used to
characterize this as "Cleaning the bottle caps off the
beach so the sand will be nice and smooth around the
whale carcasses."

Answer #3: Interspersed below.

> char var[10] = " ";
> var[9] = '\0';


First, this isn't "filling a field," it's initializing
an array and then overwriting part of it. The distinction
is important because the first line works only as an
initializer; you can't just assign arrays with the `='
operator.

Second, it's silly. You initialize `var' to contain
nine spaces followed by a zero, and then you overwrite that
zero with another zero. What's the point of the overwrite?

> or
>
> char var[10];
> sprintf( var, " " );


This is probably the slowest of the alternatives you've
presented. No guarantees, of course.

> or
>
> char var[10];
> strcpy( var, " " );
> var[9] = '\0';


Again, why set var[9] to zero twice? Are you afraid it
won't remember its value unless reminded? ("Hey there,
var[9]: Pay attention or I'll make you stay after class and
clean the erasers!")


> int i;
> char var[10];
> for(i=0; i<10; i++)
> {
> var[i] = ' ';
> }
> var[i] = '\0';


Why set var[9] to a space, only to turn right around and
overwrite the space with a zero? You could save time (HUGE
amounts of time by stopping the loop after nine iterations
instead of ten.

> Thanks for the help.


You're welcome. The biggest help I think I can be is to
suggest that you not worry about the speed of such a trivial
thing until and unless you've *measured* the performance of
your program and determined that the timing of this fragment
is crucial. (A consequence of this philosophy of "help" is
that I'm not even going to mention the manymanymany other
ways you might go about this task.)

--

 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      04-21-2004
In <c6662m$rs8$> "Justin Robbs" <> writes:

>I need to fill a number of character fields with spaces or zero's
>depending on the field. Which method is faster.


Are you absolutely sure that it makes any *prctical* difference to your
program?

>char var[10] = " ";
>var[9] = '\0';
>
>or
>
>char var[10];
>sprintf( var, " " );
>
>or
>
>char var[10];
>strcpy( var, " " );
>var[9] = '\0';
>
>or
>
>int i;
>char var[10];
>for(i=0; i<10; i++)
>{
> var[i] = ' ';
>}
>var[i] = '\0';


How about the OBVIOUS?

char var[10] = " ";

Although the null character is invisible, it's built into the string
literal used as initialiser.

For zero-filling, use the following instead:

char var[10] = { 0 };

Although it's not obvious, it will fill var with zeros. When used on a
struct, it will set each field to the right form of zero (integer zero,
floating point zero, null pointer).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email:
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      04-21-2004
Eric Sosman <> spoke thus:

>> char var[10];
>> for(i=0; i<10; i++)
>> {
>> var[i] = ' ';
>> }
>> var[i] = '\0';


> Why set var[9] to a space, only to turn right around and
> overwrite the space with a zero? You could save time (HUGE
> amounts of time by stopping the loop after nine iterations
> instead of ten.


Note that as written, it's var[10] that gets set to 0 - perhaps OP
hoped that UB in this case would result in instant completion?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      04-21-2004
In <> Eric Sosman <> writes:

>Justin Robbs wrote:
>>
>> int i;
>> char var[10];
>> for(i=0; i<10; i++)
>> {
>> var[i] = ' ';
>> }
>> var[i] = '\0';

>
> Why set var[9] to a space, only to turn right around and
>overwrite the space with a zero?


That's NOT what the code is doing. It's the non-existing var[10] that
is set to zero, to generate lots of fun at debug time

>You could save time (HUGE
>amounts of time by stopping the loop after nine iterations
>instead of ten.


Nah, that would make the code boringly correct.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email:
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-21-2004
Christopher Benson-Manica wrote:
>
> Eric Sosman <> spoke thus:
>
> >> char var[10];
> >> for(i=0; i<10; i++)
> >> {
> >> var[i] = ' ';
> >> }
> >> var[i] = '\0';

>
> > Why set var[9] to a space, only to turn right around and
> > overwrite the space with a zero? You could save time (HUGE
> > amounts of time by stopping the loop after nine iterations
> > instead of ten.

>
> Note that as written, it's var[10] that gets set to 0 - perhaps OP
> hoped that UB in this case would result in instant completion?


Good catch; I read too hastily (perhaps infected by
the O.P.'s emphasis on speed).

--

 
Reply With Quote
 
Justin Robbs
Guest
Posts: n/a
 
      04-21-2004

"Justin Robbs" <> wrote in message
news:c6662m$rs8$...
> I need to fill a number of character fields with spaces or

zero's
> depending on the field. Which method is faster.
>
> char var[10] = " ";
> var[9] = '\0';
>


I usually don't initialize my char's and forgot about the null
terminator at the end.

> or
>
> char var[10];
> sprintf( var, " " );
>
> or
>
> char var[10];
> strcpy( var, " " );
> var[9] = '\0';
>


I usually don't use strcpy and forgot about the null terminator
at the end.

> or
>
> int i;
> char var[10];
> for(i=0; i<10; i++)


Sorry, I went to fast this should obviously be for(i=0;i<9;i++)
> {
> var[i] = ' ';
> }
> var[i] = '\0';
>
> Thanks for the help.
>
> Justin
>
>


This is kind of a reply to all regarding whether this will make a
realistic difference in speed. I hope it is not bad form to
respond this way.

I am writing a routine to send Credit Card transactions to the
authorizer. Obviously the biggest speed issue will be the
network speed, however, I was just trying to cut down the amount
of time I spend preparing the transactions. It is not just one
field that needs to be set to spaces. The authorizer sent me the
record layout for the authorization request. This layout is a
catch all that contains a number of fields that I won't need.
For example, there is approximately 100 char's dedicated to
shipping address that need to be set to spaces. This is being
developed for a convenience store so shipping address is not
really necessary. There are also fields for dealing with
reversals, PIN data, and various card specific fields that will
only be used in certain situations.

I appreciate the helpful responses. I actually figured that for
loop would be slower than the function calls. I am a self taught
C programmer so I am kind of learning by necessity. I know it is
trivial to be concerned with these small amounts of time savings,
I try to use a common sense approach to making my code run
quickly and cleanly. A lot of small savings add up to larger
ones eventually. Plus, this knowledge may help make a more
significant time savings in a project further down the road.

Thanks,
Justin


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Microcontrollers: which one ? which language ? which compiler ? The Jesus of Suburbia NZ Computing 2 02-11-2006 06:53 PM
Web Stats? Which to use? Which is best? Familyman HTML 3 02-09-2006 11:05 PM
ADSL WIC support - which NM's, and which IOS versions? Kralizec Craig Cisco 5 12-08-2005 02:20 AM
which XMI version compatible to which UML version? Kenny XML 0 06-02-2004 10:20 PM
Keeping track of which user controls need to be loaded and which not John ASP .Net 0 07-08-2003 09:26 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57