Article View: harvard.comp.unix
Article #2Re: C system command
From: ari lukumies
Date: Mon, 27 Nov 1995 14:47
Date: Mon, 27 Nov 1995 14:47
39 lines
2162 bytes
2162 bytes
sarmad@scunix4.harvard.edu (Shaikh Sarmad) wrote:
>
> Thank you Joev and Eugene for your suggestions. Both of the suggestions
> work and I have solved my problem. Just wondering, which of the two
> implementations would be more efficient (not that it's a problem!)
>
>
Usually, the more efficient way is to use strcpy/strcat version.
This is because str copying is usually a few assembler language
instructions (initialize source pointer, initialize target pointer
and execute 'copy until null-terminator'), at least if the compiler
can inline these. Strcat takes a little more instructions (initialize
target pointer, execute 'find first null-terminator', initialize
source pointer and execute 'copy until null-terminator'). Even if
you rolled a function of your own:
void concat(char *tgt, char *first, char *second)
{
while (*first)
*tgt++ = *first++;
while (*second)
*tgt++ = *second++;
*tgt = '\0';
}
you'd get better response time than that of using sprintf,
because sprintf has to parse your format string, and there are
many other things for sprintf to consider in addition of the
actual copying: length restrictions, left/right alignment, different
formats (int, long, double, char, char *, ...), padding, etc. So,
the overhead using sprintf is larger, and usually using sprintf
(or any other printf/scanf family function) also adds to the size
of your program by getting floating-point support along. Of course,
if you do floating-point in your program, that's already required,
but if you just need a quick/small pgm to do something fast, I'd
prefer the strcpy/strcat style.
Ari Lukumies
Technical Details
Message-ID:
<49cj24$lfe@tahko.lpr.carel.fi>Path:
arc1-ca.pugleaf.net!news.lpr.carel.fi!usenetReferences:
<4997nh$sjj@decaxp.harvard.edu> <uufd9afhhri.fsf@scws27.harvard.edu> <49b2cg$4e3@decaxp.harvard.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.lpr.carel.fi!usenet From: ari lukumies <aril@cmt.lpr.mail.carel.fi> Newsgroups: harvard.comp.unix,comp.unix.questions,comp.lang.c Subject: Re: C system command Date: 27 Nov 1995 14:47:32 GMT Organization: Carelcomp Forest Oy Message-ID: <49cj24$lfe@tahko.lpr.carel.fi> References: <4997nh$sjj@decaxp.harvard.edu> <uufd9afhhri.fsf@scws27.harvard.edu> <49b2cg$4e3@decaxp.harvard.edu> NNTP-Posting-Host: renoir.cclahti.carel.fi