[comp.lang.c] Just a little something that has been bothering me.

sven@cs.widener.edu (Sven Heinicke) (12/09/90)

What is quicker?

	int a = 0,i = 0;

or

	int a,i;
	i = a = 0;

and why?

This type of thing has been bothering me for a long time.

-- 
sven@cs.widener.edu                                      Widener System Manager

gwyn@smoke.brl.mil (Doug Gwyn) (12/09/90)

In article <1990Dec08.222943.1581@cs.widener.edu> sven@cs.widener.edu (Sven Heinicke) writes:
-What is quicker?
-	int a = 0,i = 0;
-or
-	int a,i;
-	i = a = 0;
-and why?
-This type of thing has been bothering me for a long time.

Why would you care?  They're obviously not going to be bottlenecks..

pfalstad@phoenix.Princeton.EDU (Paul John Falstad) (12/09/90)

In article <1990Dec08.222943.1581@cs.widener.edu> sven@cs.widener.edu (Sven Heinicke) writes:
>What is quicker?
>
>	int a = 0,i = 0;
>
>or
>
>	int a,i;
>	i = a = 0;

Depends.  If these are local variables, then most likely they will
produce the same code.  The latter case is simply postponing the
initialization.

If these are global variables, the former case is quicker (in UNIX,
anyway), since the compiler doesn't have to generate any code to
initialize the two variables.  In the latter case, it puts a and i in
bss, but in the former case, it puts them in the initialized data segment.

Your implementation could be totally different however.  :-)

--
From somewhere in front, you hear a repulsive squelching mumble, as if a
chattering orangutan has suddenly had its mouth crammed full of earthworms.
"You're invading my personal space!" shrieks the travel agent, rearing back
from you like an offended snake.  She is probably from California.

fjb@metaware.metaware.com (Fred Bourgeois) (12/11/90)

In article <14694@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
>In article <1990Dec08.222943.1581@cs.widener.edu> sven@cs.widener.edu (Sven Heinicke) writes:
>-What is quicker?
>-      int a = 0,i = 0;
>-or
>-      int a,i;
>-      i = a = 0;
>-and why?
>-This type of thing has been bothering me for a long time.
>Why would you care?  They're obviously not going to be bottlenecks..

... and if they *still* bother you, try generating assembly listing and
see what the compiler does with them.  ;-)
-- 
Fred Bourgeois, MetaWare Inc., 2161 Delaware Avenue, Santa Cruz, CA 95060-2806
fjb@metaware.com					...!uunet!metaware!fjb
	     Colorless Green Ideas Sleep Furiously, and so do I.

c145gmk@utarlg.utarl.edu (GORDON KEEGAN) (12/11/90)

In article <1990Dec08.222943.1581@cs.widener.edu>, sven@cs.widener.edu (Sven Heinicke) writes...
>What is quicker?
> 
>	int a = 0,i = 0;
> 
>or
> 
>	int a,i;
>	i = a = 0;
> 
>and why?
> 
>This type of thing has been bothering me for a long time.


	Our C compiler under VMS 5.3-1 produced the following
	assembler code for the respective C segments:

	    int a=0, i=0;
                 F8 AD 00 D0    000B		movl	#0,-8(fp)
                 F4 AD 00 D0    000F		movl	#0,-12(fp)

	and

	    int a, i;
	    i = a = 0;
                 F8 AD 00 D0    000B		movl	#0,-8(fp)
              F4 AD F8 AD D0    000F		movl	-8(fp),-12(fp)

	The second will be marginally slower, but only just...

-----------------------------------------------------------------------------
|  Gordon Keegan                    ||   Bitnet  : c145gmk@utarlg           |
|  Systems Programmer               ||   THEnet  : UTARLG::C145GMK          |
|  Academic Computing Services      ||   Internet: c145gmk@utarlg.utarl.edu |
|  University of Texas, Arlington   ||   AT&TNet : 817-273-2208             |
-----------------------------------------------------------------------------
|  Beauty does what beauty does best: it's beautiful...                     |
-----------------------------------------------------------------------------

userAKDU@mts.ucs.UAlberta.CA (Al Dunbar) (12/11/90)

In article <1990Dec08.222943.1581@cs.widener.edu>, sven@cs.widener.edu (Sven Heinicke) writes:
>What is quicker?
>
>        int a = 0,i = 0;
>
>or
>
>        int a,i;
>        i = a = 0;
>
>and why?
>
>This type of thing has been bothering me for a long time.
>
>--
>sven@cs.widener.edu                                      Widener System Manager
 
The correct answer to the first question is "the one that takes
less time to execute". At least that is about all that can be
said until you say what compiler you are using. Once you've said
that, the answer can be further refined by using a stopwatch.
 
Since the first question cannot be answered, neither can the
second, except to say "because it takes less time to execute".
 
having said the above, a simple compiler *might* generate quicker
code for the second case, because once the zero value is
generated in a register to assign to a, it needn't be generated
again. I believe, however, that most compilers worth the diskette
they are sold on would optimize these fragments into identical
code.
 
-------------------+-------------------------------------------
Al Dunbar          |
Edmonton, Alberta  |  "this mind left intentionally blank"
CANADA             |          - Manuel Writer
-------------------+-------------------------------------------

harrison@necssd.NEC.COM (Mark Harrison) (12/11/90)

In article <14694@smoke.brl.mil>, gwyn@smoke.brl.mil (Doug Gwyn) writes:
> In article <1990Dec08.222943.1581@cs.widener.edu> sven@cs.widener.edu (Sven Heinicke) writes:
> -What is quicker?
> -This type of thing has been bothering me for a long time.

> Why would you care?  They're obviously not going to be bottlenecks..

Maybe he isn't worried about the speed, but about understanding how
the compiler works.
-- 
Mark Harrison             harrison@necssd.NEC.COM
(214)518-5050             {necntc, cs.utexas.edu}!necssd!harrison
standard disclaimers apply...

bright@nazgul.UUCP (Walter Bright) (12/12/90)

In article <1990Dec08.222943.1581@cs.widener.edu> sven@cs.widener.edu (Sven Heinicke) writes:
-What is quicker?
-	int a = 0,i = 0;
-or
-	int a,i;
-	i = a = 0;
-and why?

Why not:
1. Compile and disassemble the output, and see if there is any difference
   in the code generated.
2. Write a simple timing loop to see which is faster.

ccastdf@prism.gatech.EDU (Dave) (12/13/90)

bright@nazgul.UUCP (Walter Bright) writes:

>In article <1990Dec08.222943.1581@cs.widener.edu> sven@cs.widener.edu (Sven Heinicke) writes:
>-What is quicker?
>-	int a = 0,i = 0;
>-or
>-	int a,i;
>-	i = a = 0;
>-and why?


Well, int a=0,i=0 generates the following lines of assembly:

_main:
	jmp	.L22
.L21:
	movl	$0,-4(%ebp)
	movl	$0,-8(%ebp)

and int a,i; i=a=0; generates these lines:

_main:
	jmp	.L22
.L21:
	movl	$0,-4(%ebp)
	movl	$0,%eax
	movl	%eax,-8(%ebp)



Therefore, on a 386 Sequent with no optimization, the first is more effieient.


Dave
-- 
David Frascone 
Georgia Institute of Technology,
Atlanta Georgia, 30332
Office of Computing Services--User Assistant

bright@nazgul.UUCP (Walter Bright) (12/15/90)

In article <18539@hydra.gatech.EDU> ccastdf@prism.gatech.EDU (Dave) writes:
/Therefore, on a 386 Sequent with no optimization, the first is more effieient.

Why are you concerned with which is more efficient if you turn off optimization?

rlf@dptspd.sat.datapoint.com (Rory Foster) (12/19/90)

sven@cs.widener.edu (Sven Heinicke) writes:

>What is quicker?

>	int a = 0,i = 0;

>or

>	int a,i;
>	i = a = 0;

>and why?

>This type of thing has been bothering me for a long time.


Well, I looked at it and (without optimizing) the first case was "quicker".
I was using the UNIX SVR4 C compiler.  If your compiler has an assembly option,
(-S in Johnson's PCC) use it to see for yourself.

When I optimize, my compiler shows no difference.  So there you have it,
it sort of just depends on what you're using at the time.

Regards,

Rory Foster
rlf@dptspd.sat.datapoint.com

zsinwatt@qut.edu.au (James Watt) (12/19/90)

In article <rlf.661536169@dptspd>, rlf@dptspd.sat.datapoint.com 
(Rory Foster) writes:

> sven@cs.widener.edu (Sven Heinicke) writes:
> 
>>What is quicker?
> 
>>	int a = 0,i = 0;
> 
>>or
> 
>>	int a,i;
>>	i = a = 0;
> 
>>and why?
> 
>>This type of thing has been bothering me for a long time.
> 
> 
> Well, I looked at it and (without optimizing) the first case was "quicker".
> I was using the UNIX SVR4 C compiler.  If your compiler has an assembly option,
> (-S in Johnson's PCC) use it to see for yourself.
> 
> When I optimize, my compiler shows no difference.  So there you have it,
> it sort of just depends on what you're using at the time.

On a HP9000 840 ( without optimisation), one is better than two, and 
with optimization they are basically similar. However if you use the
int a,i=0; form of declaration and optimize it, it outperforms both
one and two. In reality there are far too many variables to make this 
kind of exercise have any real relevance to the meaning of life in general.


-- 
|----------------------------------------------------------------------------|
| James Watt | AARnet: james@water.fit.qut.edu.au ARPA: ZSINWATT@QUT.EDU.AU  |
|----------------------------------------------------------------------------|