Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Difference between int i, j; and int i; int j;

Reply
Thread Tools

Difference between int i, j; and int i; int j;

 
 
arun
Guest
Posts: n/a
 
      07-24-2006
Hi all,,

I want to know if there is any performance difference between the
following sets of declarations.

int i, j;

and

int i;
int j;

Thanks,
arun,,

 
Reply With Quote
 
 
 
 
Ico
Guest
Posts: n/a
 
      07-24-2006
arun <(E-Mail Removed)> wrote:
> Hi all,,
>
> I want to know if there is any performance difference between the
> following sets of declarations.
>
> int i, j;
>
> and
>
> int i;
> int j;


There might, or there might not; this depends completely on your
implementation. If you want to know, measure it. Or - if your toolchain
allows you to - look at the generated assembly code to see if there is
any difference.



--
:wq
^X^Cy^K^X^C^C^C^C
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      07-24-2006
"arun" <(E-Mail Removed)> wrote:

> I want to know if there is any performance difference between the
> following sets of declarations.
>
> int i, j;
>
> and
>
> int i;
> int j;


Wrong question entirely.

Imprimis, it's impossible to say anything at all with the absolute
certainty you probably suspect without knowing the exact details of a.
the compiler b. the platform you compile on c. the platform you run the
program on d. the options used when compiling e. the rest of the code f.
the configuration of any machine involved and g. the phase of the moon.

Secundis, even with all that knowledge, indeed even without it, it is
quite obvious that any reasonable compiler would compile both of those
snippets completely identically, _but_ you're not guaranteed that your
compiler is reasonable.

Tertiis, unless you know with 99.99% certainty that the difference is a
real problem for you, don't worry about it. Premature optimisation is
the root of much evil.

Richard
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      07-24-2006
arun wrote:

> Hi all,,
>
> I want to know if there is any performance difference between the
> following sets of declarations.
>
> int i, j;
>
> and
>
> int i;
> int j;


The C standard doesn't say anything about performance.

Whether or not there is a difference depends on the particular implementation.
Suck it and see.

If there /were/ a difference, I would be gobsmacked. When I picked my jaw
back up off the floor, I would likely switch to a saner compiler.

--
Chris "detachable jaw" Dollin
"A facility for quotation covers the absence of original thought." /Gaudy Night/

 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      07-24-2006

arun wrote:
> Hi all,,
>
> I want to know if there is any performance difference between the
> following sets of declarations.
>
> int i, j;
>
> and
>
> int i;
> int j;
>
> Thanks,
> arun,,


The only way to know for sure is to compile and run both versions and
use a profiling tool to compare performance. Having said that, I can't
imagine that there would be a measurable difference in runtime
performance between the two (a sane compiler should generate
essentially the same machine code for the two versions), and I doubt
it would have a measurable effect on build times either.

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      07-24-2006
arun wrote:

> Hi all,,
>
> I want to know if there is any performance difference between the
> following sets of declarations.
>
> int i, j;
>
> and
>
> int i;
> int j;


The first uses ten characters (including a newline), while
the second uses fourteen. If you use the first form in preference
to the second often enough, the four-byte savings will eventually
accumulate to the point where your editor fewer disk I/O operations
to write the file and your compiler uses fewer I/Os to read it.
I/O is vastly slower than almost anything else a computer, so the
first form is faster. (It also takes less typing, which is an
an even greater savings.)

Aren't you glad you asked? To what use will you put your
newfound knowledge?

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      07-24-2006
arun a écrit :
> Hi all,,
>
> I want to know if there is any performance difference between the
> following sets of declarations.
>
> int i, j;
>
> and
>
> int i;
> int j;
>
> Thanks,
> arun,,
>


Normally there will be never any difference.

Subtle processor dependent features can make a difference
in the stack layout, but not in this case where the layout
is probably the same.

In some processors
(1)
int fn(void)
{
char buf[2024];
int counter;
}

will make a performance difference with
(2)
int fn(void)
{
int counter;
char buf[2024];
}

If the stack variable "counter" is in the stack and is used a lot,
each access will use a shorter instruction in case (2), since
the offset from the frame-pointer is less than 128 bytes and
can use shorter instructions.

This could make 0.000001% of efficiency gain in some processors.
This supposes a compiler that does NOT optimize this by
making stack layout independent from declaration order!

Short answer:

Do not worry about this kind of details.

jacob

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      07-24-2006
On Mon, 24 Jul 2006 15:00:04 +0200, in comp.lang.c , jacob navia
<(E-Mail Removed)> wrote:

>Subtle processor dependent features can make a difference
>in the stack layout, but not in this case where the layout
>is probably the same.


This bit is perhaps true, but dangerous to put first.

>Short answer:
>
>Do not worry about this kind of details.


This is the bit to put first - many people would not have read this
far, and would therefore have missed the best advice !
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      07-31-2006
On Mon, 24 Jul 2006 15:00:04 +0200, jacob navia
<(E-Mail Removed)> wrote:
<snip>
> In some processors
> (1)
> int fn(void)
> {
> char buf[2024];
> int counter;
> }
>
> will make a performance difference with
> (2)
> int fn(void)
> {
> int counter;
> char buf[2024];
> }
>
> If the stack variable "counter" is in the stack and is used a lot,
> each access will use a shorter instruction in case (2), since
> the offset from the frame-pointer is less than 128 bytes and
> can use shorter instructions.
>

Unless the compiler is halfdecent (most are nowadays) and the
processor is also (x86 isn't) then it will keep counter in a register
and not in the stack at all. <pedantic> Assuming of course there is a
single stack and automatic variables are (normally) in it, which are
common but not required and not universal. </>

And not on Tandem NonStop where the first will (naively) use
counter L+1
buf L+2,S or L+3,S or L+2,X or L+3,X depending on modes
and the latter will use
buf L+1,S or L+1,X depending on modes
counter L+2 or L+3 depending on modes
and the code generated will do exactly the same number of virtual
accesses either way; physical performance may be affected by cache
differences, but that could go either way depending on a large variety
of factors that can't practically be predicted or probably even
measured, and similarly on many (most?) other platforms.

Although there are _other_ cases where ordering of declarations would
make a difference on TNS, some of which would make _no_ difference on
the platforms/implementations you are familiar with.

> This could make 0.000001% of efficiency gain in some processors.
> This supposes a compiler that does NOT optimize this by
> making stack layout independent from declaration order!
>

or use registers, as above.

> Short answer:
>
> Do not worry about this kind of details.
>

Agree there.

- David.Thompson1 at worldnet.att.net
 
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
What is the difference between: int a (*daytab)[13] and int a*daytab[13] magicman C Programming 5 05-04-2008 08:46 PM
difference between: int *a[10][10] and int (*a)[10][10] benj C++ 2 03-26-2007 09:00 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Difference between int() and (int) James Aguilar C++ 5 10-11-2004 02:41 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments