473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Which is faster?

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
Nov 14 '05 #1
25 2268
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

Nov 14 '05 #2
Justin Robbs <ju************@spamhotmail.com> 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.
Nov 14 '05 #3
In article <c6**********@news.tdl.com>,
Justin Robbs <ju************@SPAMhotmail.com> 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.
Nov 14 '05 #4
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.)

--
Er*********@sun.com
Nov 14 '05 #5
Eric Sosman <Er*********@sun.com> 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.
Nov 14 '05 #6
In <c6**********@news.tdl.com> "Justin Robbs" <ju************@SPAMhotmail.com> 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: Da*****@ifh.de
Nov 14 '05 #7
In <40***************@sun.com> Eric Sosman <Er*********@sun.com> 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: Da*****@ifh.de
Nov 14 '05 #8

"Justin Robbs" <ju************@SPAMhotmail.com> wrote in message
news:c6**********@news.tdl.com...
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
Nov 14 '05 #9

"Dan Pop" <Da*****@cern.ch> wrote in message
news:c6**********@sunnews.cern.ch...
In <40***************@sun.com> Eric Sosman <Er*********@sun.com> 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.


Where is the fun of programming without tiny buffer overflows
that are difficult to detect and cause weird problems that make
you bang your head against the wall? I actually prefer the
larger ones that cause segmentation violation errors. It is
really fun to code for several days, set up a test and watch it
go splat.

Justin
Nov 14 '05 #10
Christopher Benson-Manica wrote:

Eric Sosman <Er*********@sun.com> 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).

--
Er*********@sun.com
Nov 14 '05 #11
In <c6**********@news.tdl.com> "Justin Robbs" <ju************@SPAMhotmail.com> writes:

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.
If you can map this layout with a C structure, you can create an
initialiser object of that type and perform all the initialisations
with a simple struct assignment. Hard to beat, performance-wise.
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.
Or not... Pay attention to the parts of the code requiring most of the
CPU cycles and optimise them at algorithm level, if possible, rather than
at implementation level. This is where the significant savings come from.
Plus, this knowledge may help make a more
significant time savings in a project further down the road.


The time spent in initialisations hardly matters in most real world
projects...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12
Justin Robbs wrote:
.... snip ...
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.


Then, unless memory space is critical, the following might have
the minimum futzing about, and might also be the fastest method:

typedef struct rcd {
/* fields */
} rcd;

rcd blankrcd = { /* initializer statements */

......

whatever function initandsend(/* datafields */)
{
rcd therecord;

rcd = blankrcd;
/* code to set only needed fields */
/* code to send it out */
}

--
"I'm a war president. I make decisions here in the Oval Office
in foreign policy matters with war on my mind." - Bush.
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.

Nov 14 '05 #13

"CBFalconer" <cb********@yahoo.com> wrote in message
news:40***************@yahoo.com...
Justin Robbs wrote:
... snip ...

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.


Then, unless memory space is critical, the following might have
the minimum futzing about, and might also be the fastest

method:
typedef struct rcd {
/* fields */
} rcd;

rcd blankrcd = { /* initializer statements */

.....

whatever function initandsend(/* datafields */)
{
rcd therecord;

rcd = blankrcd;
/* code to set only needed fields */
/* code to send it out */
}


That, my friend, is an excellent solution. All this talk about
improving the algorythm had me thinking about how to do that. I
could figure out any good solution other than manually
initializing the fields as described in the other posts. That is
just what I was looking for.

Have a good one.

Justin
Nov 14 '05 #14

"Dan Pop" <Da*****@cern.ch> wrote in message
news:c6**********@sunnews.cern.ch...
In <c6**********@news.tdl.com> "Justin Robbs" <ju************@SPAMhotmail.com> writes:
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 amountof time I spend preparing the transactions. It is not just onefield that needs to be set to spaces. The authorizer sent me therecord 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 willonly be used in certain situations.
If you can map this layout with a C structure, you can create

an initialiser object of that type and perform all the initialisations with a simple struct assignment. Hard to beat, performance-wise.
I read CBFalconer's response first but if I am understanding what
you are saying, it appears to be the same suggestion. Thanks for
the help.
I appreciate the helpful responses. I actually figured that forloop would be slower than the function calls. I am a self taughtC programmer so I am kind of learning by necessity. I know it istrivial 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.


Or not... Pay attention to the parts of the code requiring

most of the CPU cycles and optimise them at algorithm level, if possible, rather than at implementation level. This is where the significant savings come from.
Plus, this knowledge may help make a more
significant time savings in a project further down the road.
The time spent in initialisations hardly matters in most real

world projects...


I was just going on the theory of I could drive a semi to work or
a hybrid car, but since I only live 3/4 of a mile from work it
makes precious little difference to the environment. However, I
still feel like I am doing my part by not driving a giant fuel
guzzler. The same thinking applies here. I don't want to incur
a performance penalty when I can just code it efficiently from
the beginning. Were I trying to fix a performance bottle neck
this would be a drop in the bucket? However, since this is all
new code, I might as well do it right from the beginning.

Thanks,
Justin
Nov 14 '05 #15
Justin Robbs wrote:
[How to initialise an array of characters most quickly]
I don't want to incur a performance penalty when I can just code it
efficiently from the beginning. [...] However, since this is all new
code, I might as well do it right from the beginning.
Doing it correctly means doing the obvious thing, which could save time
for you and anyone trying to comprehend it; this time can be used to do
the real optimisations, if you like.
I was just going on the theory of I could drive a semi to work or
a hybrid car, but since I only live 3/4 of a mile from work it
makes precious little difference to the environment.


I suppose that walking a mile and a half every day is completely out of
the question, then?

--
++acr@,ka"
Nov 14 '05 #16
Eric Sosman <Er*********@sun.com> spoke thus:
Good catch; I read too hastily (perhaps infected by
the O.P.'s emphasis on speed).


Well, to be fair, John Carson's post is what showed it to me, so
(unfortunately) I can't claim any credit.

--
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.
Nov 14 '05 #17
On Wed, 21 Apr 2004 13:46:29 -0600, Justin Robbs wrote:

[snip optimizing program]
I was just going on the theory of I could drive a semi to work or
a hybrid car, but since I only live 3/4 of a mile from work it
makes precious little difference to the environment. However, I
still feel like I am doing my part by not driving a giant fuel
guzzler.
Suppose the building of a hybrid car cause more pollution than
building a giant fuel guzzler. Then suppose the saved pollution during
the lifetime of the car is smaller than the extra pollution in
production.

(This is probably not the case, I'm just adapting your analogy to fit
what is often the case in the world of programming)
The same thinking applies here. I don't want to incur
a performance penalty when I can just code it efficiently from
the beginning.
Often programmer time is much more expensive than CPU time, (the
production vs use analogy), don't waste time micromanaging code unless
you _know_ that the performance of that particular piece of code is
critical.
Were I trying to fix a performance bottle neck
this would be a drop in the bucket? However, since this is all
new code, I might as well do it right from the beginning.


And Dan and CBFs suggestion of using a struct is great for making it
right from the beginning. It's certainly easier (and thus less likely
that you make an error) than manually initializing each and every
field.

Also the suggestion to paying attention to the algorithm before
micromanaging code is a good one, it doesn't help finding the pirfect
tire rubber mixture to make a car go faster if the problems ist that
the wheels are square.

Crossposted and followups set to comp.programming since this no longer
has anything to do specifically with C

--
NPV

What did that old blonde gal say? -- That is the part you throw away.
Tom Waits - The part you throw away

Nov 14 '05 #18

"Nils Petter Vaskinn" <me@privacy.net> wrote in message
news:pa****************************@privacy.net...
On Wed, 21 Apr 2004 13:46:29 -0600, Justin Robbs wrote:

[snip optimizing program]
I was just going on the theory of I could drive a semi to work or a hybrid car, but since I only live 3/4 of a mile from work it makes precious little difference to the environment. However, I still feel like I am doing my part by not driving a giant fuel guzzler.
Suppose the building of a hybrid car cause more pollution than
building a giant fuel guzzler. Then suppose the saved pollution

during the lifetime of the car is smaller than the extra pollution in
production.

(This is probably not the case, I'm just adapting your analogy to fit what is often the case in the world of programming)
The same thinking applies here. I don't want to incur
a performance penalty when I can just code it efficiently from the beginning.
Often programmer time is much more expensive than CPU time,

(the production vs use analogy), don't waste time micromanaging code unless you _know_ that the performance of that particular piece of code is critical.
Were I trying to fix a performance bottle neck
this would be a drop in the bucket? However, since this is all new code, I might as well do it right from the beginning.
And Dan and CBFs suggestion of using a struct is great for

making it right from the beginning. It's certainly easier (and thus less likely that you make an error) than manually initializing each and every field.

Also the suggestion to paying attention to the algorithm before
micromanaging code is a good one, it doesn't help finding the pirfect tire rubber mixture to make a car go faster if the problems ist that the wheels are square.


I understand the point about finding a better algorithm. My
point was that since I am starting from scratch, I should start
with a good foundation. While my search didn't yield much of a
benefit from the performance standpoint. I did find a much
better way to get the job done. This new method is both faster
and more reliable (due to the reduced chance for programmer
error). Therefore, I think my search has been fruitful and it
was well worth my time posting.

I didn't get the answer I expected, but I did get a solution that
is better on several levels. I don't see what the problem is.
In a different situation I would obviously focus on the algorithm
for a performance increase, but the algorithm was simple here.
Get the transaction data and card data, format it correctly, and
send it to the card host. I am just trying to perform these
steps in the best way.

Thanks,
Justin
Nov 14 '05 #19
One of my favorite CS Quotes and a good point of wisdom:

"Premature optimization is the root of all evil
(or at least most of it) in programming."

- Donald Knuth

Nov 14 '05 #20
"Justin Robbs" <ju************@SPAMhotmail.com> wrote:

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.


Your primary concern is application financial integrity.
You should write the simplest, clearest, most portable,
most maintainable code possible. For example:
char x[10] = " ";
is bad because you can't see at a glance whether it is correct
or not, and it doesn't hold up well to the dimensions of x changing.
This would be even worse if x were a member of a struct, and the
" " were part of a struct initialization.
I also suggest you learn basic things like the fact that string
literals are 0-terminated, and strcpy() works with 0-terminated strings.

It won't be a good look if your customer comes back to you saying
"Why did my card get charged twice", and you say "Dunno but the
terminal only took 14.999 seconds instead of 15 seconds to process
the transaction"
Nov 14 '05 #21
On Wed, 21 Apr 2004 18:27:17 +0200,
Ahmed S. Badran <ah**********@spymac.com> wrote:

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


Perhaps, perhaps not. Compilers may recognize this as a known function and
inline it as a series of moves just like the first example, especialy when
the size of the source string is known at compile time. The second example
may also be recognized as a special case of sprintf and substitute strcpy
and inline it.

Compiler optimizations may make all assumptions of which is faster
invalid, so if someone realy wan't to know for a particular compiler
on a particular computer model using a particular optimization option,
you need to benchmark it.
Villy
Nov 14 '05 #22
"CBFalconer" <cb********@yahoo.com> wrote in message
news:40***************@yahoo.com...
[snip]
typedef struct rcd {
/* fields */
} rcd;

rcd blankrcd = { /* initializer statements */
Possibly 'static' at file scope, or 'static' within initandsend() - ie, use
the smallest scope sensible. In either case, 'const' would make sense.
.....

whatever function initandsend(/* datafields */)
{
rcd therecord;

rcd = blankrcd;
/* code to set only needed fields */
/* code to send it out */
}

Nov 14 '05 #23
In article <c6**********@news.tdl.com>,
"Justin Robbs" <ju************@SPAMhotmail.com> wrote:
I am writing a routine to send Credit Card transactions to the
authorizer.


If you are doing that, and you are asking on comp.lang.c for help, then
I am slightly worried.
Nov 14 '05 #24

"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in
message
news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <c6**********@news.tdl.com>,
"Justin Robbs" <ju************@SPAMhotmail.com> wrote:
I am writing a routine to send Credit Card transactions to the authorizer.
If you are doing that, and you are asking on comp.lang.c for

help, then I am slightly worried.


Why? I am a fairly competent C programmer. I have always been
able to get the computer to do what I want it to do. I just
thought I would take some extra time to ensure that I am going
about this in the best way. Since I have taught myself, I figure
if I am not sure of something I should ask for advice. My
teaching has been by necessity so while I actually know some
reasonably advanced stuff, there are some areas where my skills
need refining. I have never had to worry much about performance,
but I just wanted to ensure I wasn't using a sledge hammer on a
push pin.

I have the tools, time and facilities to test this quite
thoroughly before it goes into production. Even the most
experienced guru's (not that I necessarily am in that group)
encounter problems they need help with. That's partly why usenet
was created. The wisest people in the world are those who know
when they need help.

Besides, it is not like the process is overly difficult.
Customer swipes card, program reads card, send card and
transaction data to authorizer, and wait for response.
Obviously, I will be testing very thoroughly due to the sensitive
nature of what I am doing, but that doesn't mean the process is
that difficult.
Nov 14 '05 #25

On Thu, 22 Apr 2004, Justin Robbs wrote:

"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote...
"Justin Robbs" <ju************@SPAMhotmail.com> wrote:
I am writing a routine to send Credit Card transactions to
the authorizer.
If you are doing that, and you are asking on comp.lang.c for
help, then I am slightly worried.


Why? I am a fairly competent C programmer. I have always been
able to get the computer to do what I want it to do.

[...] Besides, it is not like the process is overly difficult.
Customer swipes card, program reads card, send card and
transaction data to authorizer, and wait for response.
Obviously, I will be testing very thoroughly due to the sensitive
nature of what I am doing, but that doesn't mean the process is
that difficult.


I do believe Christian's point is that if you cannot by yourself
figure out things like variable initialization in the relatively
straightforward C programming language, then you probably oughtn't
to be messing around with real people's sensitive data over publicly-
eavesdroppable telephone lines.
The C language can be remarkably unforgiving when it comes to
things like debugging. Watch out for buffer overflows and un- or mis-
initialized variables.
Even besides the pitfalls of writing a complex program in C,
you'll have to deal with the pitfalls of real cryptography. This
is a non-trivial field, even if you are planning to use a pre-rolled
and open-source crypto library for the dirty stuff.
There is already a whole lot of code out there in the world that
tries to do serious stuff and fails miserably because the programmers
involved did not understand what they were getting into. (Cf. Microsoft,
Diebold.) Please, please, *please* make sure you're not going to be
contributing to that supply!

HTH,
-Arthur

Nov 14 '05 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
5
by: MLH | last post by:
I have a table I can open as table type recordset or a dynaset. Searching for a particular value in the table's main keyfield, which would be faster and less strain on the application......
18
by: junky_fellow | last post by:
which of the following is faster and why ? if ( x == 1 ) { } or if ( x != 0 ) {
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
7
by: Sunil Varma | last post by:
Is accessing function by it's name faster or accessing it by its address faster?
4
by: Sonnich | last post by:
Hi I have a costum function for a special search, which sort strings. This is currently the place where I can save a lot of time (~70%) if possible. So, which is faster: for($j =...
25
by: Ganesh | last post by:
Hi, This is a question that pertains to pointers in general (C or C++). Which of the following is faster and why? for (int i = 0; i < N; i++) = ... a... (or)
8
by: Sing | last post by:
Dear C Gurus, I would like to optimise a max() algo that my program uses many times. Which one is faster or are they the same? 1. #define max(a,b) (((a) (b)) ? (a) : (b)) 2. if (a>b) return a...
36
by: lak | last post by:
Which is faster? Post Increment or assignment? Why? I was not able to get any things.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.