473,388 Members | 1,427 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,388 software developers and data experts.

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

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,,

Jul 24 '06 #1
8 2368
Ico
arun <ar****************@gmail.comwrote:
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
Jul 24 '06 #2
"arun" <ar****************@gmail.comwrote:
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
Jul 24 '06 #3
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/

Jul 24 '06 #4

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.

Jul 24 '06 #5
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
es*****@acm-dot-org.invalid
Jul 24 '06 #6
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

Jul 24 '06 #7
On Mon, 24 Jul 2006 15:00:04 +0200, in comp.lang.c , jacob navia
<ja***@jacob.remcomp.frwrote:
>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
Jul 24 '06 #8
On Mon, 24 Jul 2006 15:00:04 +0200, jacob navia
<ja***@jacob.remcomp.frwrote:
<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. <pedanticAssuming 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
Jul 31 '06 #9

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

Similar topics

34
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. ...
21
by: b83503104 | last post by:
Hi, Can someone tell me the difference between single quote and double quote? Thanks
26
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: ...
21
by: Rich | last post by:
I was considering C# for developing a scientific application, but I have noticed a ~30% difference between VC++ .NET and C# on the same machine, under identical conditions: double a = 0,b = 0, c...
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
3
by: bbawa1 | last post by:
Hi, I have a table which has a field ItemsReceived of type datetime. I have a grid view which has two columns. In first column i have to show the data from field ItemsReceived and in second...
12
by: Petronius | last post by:
Hallo, does anyone have an idea how to implement difference lists in Javascript? Thanks allot in advance
5
by: Julius | last post by:
Hej dudes, I need to calc the difference between two timestamps / dates ... For example what i need to calculate: Date 1: 2007.11.06 - 20:13:04 Date 2: 2007.11.07 - 21:13:04 Difference:...
9
by: viki1967 | last post by:
Hi all! This new forum its great! :) Congratulations !!! My answer: why this my code not working? Nothing error but not work the difference.... : <html>
11
by: cmb3587 | last post by:
I have two arrays and I'm trying to create a 3rd array that is the difference between the two arrays Ex: arrayA: 3 5 8 9 arrayB: 3 4 6 9 difference of A-B: 5 8 however, my...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.