LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-29-2005, 04:54 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,485

Rep: Reputation: 66
performance


Im reading a rather dated book to try and get a better understanding
and experiance with C.

Its come to the point about performance in a c program.

what im wondering is if the new compilers automaticly adjust things so that
performace is alredy included in the program or I should follow recomendations
in the book.

for example we have come to where we incrament and decrament. Now
I know this part of the C language and im just skiming over these parts
but they show 2 examples of how to perform this and say that one faster
when exectuing then the other.

ex. c = c + 1; < -- this is the slower way
ex. c += 1; < -- supposed to execute faster


so back to my original question is on a newer compiler will there be any diffrence between these ?
 
Old 07-29-2005, 05:15 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Optomizations on statements like those are commonplace. Stuff that you do need to concentrate on is efficient algorithms.

It's nice that the compiler takes care of little syntactical optomizations for you, and it can also take care of some algorithm deficiencies for you, but you're ultimately responsible for the major algorithm decisions such as using a proper sorting routine.

To answer your questions spefically, it's highly doubtful that those 2 statements will produce different code once the compiler has a go at it. You may want to look at your compiler manual for a way to make it spit out the assembly language it will use. With gcc you can type gcc -S myprog.c and it will create a myprog.s that contains the assembly instructions. You can create 2 programs, one that uses c = c + 1 and one that uses c += 1 and compare the two resulting .s files to verify that the compiler is optomizing it.

Last edited by itsme86; 07-29-2005 at 05:18 PM.
 
Old 07-29-2005, 05:43 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,500

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
That one will run faster
Code:
c++;
Quote:
The apcolypse ...
Please, check your speling ...
 
Old 07-29-2005, 06:04 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Quote:
Originally posted by jlliagre
That one will run faster
Code:
c++;
I can pretty much guarantee you that the compiler will automagically generate the same code for c = c + 1 and c++
 
Old 07-29-2005, 06:54 PM   #5
evilmrhenry
Member
 
Registered: Oct 2003
Distribution: Knoppix
Posts: 103

Rep: Reputation: 15
I just compiled (using gcc) three programs:

Code:
#include <stdio.h>
int main(void)
{
  int c;
  c = c + 1;
  exit(0);
}
Code:
#include <stdio.h>
int main(void)
{
  int c;
  c += 1;
  exit(0);
}
Code:
#include <stdio.h>
int main(void)
{
  int c;
  c++;
  exit(0);
}
and ran md5sum on all of them. They are identical when using gcc.
 
Old 07-29-2005, 07:02 PM   #6
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,485

Original Poster
Rep: Reputation: 66
cool thanks.

i created similar programs and did a time on each they are the same as well.
 
Old 07-29-2005, 08:16 PM   #7
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Compilers are smart.... What's always amazed me is optimizations on REALLY BAD code like this:
1.c:
Code:
$ cat 1.c
#include <stdio.h>

int main(int argc,char **argv){
        goto a;
        if(0){
                a:
                printf("hello world!\n");
        }
        return 0;
}
$ gcc -o 1 1.c
$ strings 1
/lib/ld-linux.so.2
libc.so.6
printf
_IO_stdin_used
__libc_start_main
__gmon_start__
GLIBC_2.0
PTRh
hello world!
2.c:
Code:
$ cat 2.c
#include <stdio.h>

int main(int argc,char **argv){
        if(0){
                a:
                printf("hello world!\n");
        }
        return 0;
}
$ gcc -o 2 2.c
$ strings 2
/lib/ld-linux.so.2
libc.so.6
_IO_stdin_used
__libc_start_main
__gmon_start__
GLIBC_2.0
PTRh
QVhT
Lookie... if statement with constant false value AND NO JUMPS IN... optimizes the code out (no printf or hello world).
 
Old 07-29-2005, 09:34 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,500

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Quote:
I just compiled (using gcc) three programs:
These three programs do not use the "c" variable, so any smart compiler, and I hope gcc is one of them will remove this useless variable and its incrementation when any optimisation flag is set, and perhaps even without it. So beware not comparing three programs that only do an exit(0) ...

Anyway, the c=c+1 vs c +=1 vs c++ is so trivial all compilers optimize them to the same code since ages.
 
Old 07-29-2005, 10:51 PM   #9
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
This is my "pseudo-study". In all cases, the code was the following (for 1.c, 2.c, and 3.c):
Code:
$ cat 1.c
#include <stdio.h>

int main(int argc,char **argv){
        int c=0;
        c++;
        printf("%d\n",c);
        return 0;
}

$ cat 2.c
#include <stdio.h>

int main(int argc,char **argv){
        int c=0;
        c+=1;
        printf("%d\n",c);
        return 0;
}

$ cat 3.c
#include <stdio.h>

int main(int argc,char **argv){
        int c=0;
        c=c+1;
        printf("%d\n",c);
        return 0;
}
From now on, N refers to each of 1, 2, and 3.

Using 'gcc -o N N.c'
Code:
$ md5sum 1 2 3
7719321b79400b5e9c415b48c73d9063  1
5b3ed8a260fbca41b45a39daeff52402  2
e1272be246cae6f357ceaa686df553b5  3
Using 'gcc -O3 -o N N.c'
Code:
$ md5sum 1 2 3
b70ea86b51993c447c97ae5bd1d04aa4  1
7b98f8aa012aadca7207d6755167bd16  2
5a680a414421422d549379059b859feb  3
So, it would appear that they DON'T become quite the same... hrrm.

Ok, well, perhaps there's something odd inside. Let's make them the most compact ELF binary we can. To this end, I performed 'strip N' on each of the binaries compiled with -O3.
Code:
$ md5sum 1 2 3
88362276b5f525f15f5c89a331cbb76f  1
88362276b5f525f15f5c89a331cbb76f  2
88362276b5f525f15f5c89a331cbb76f  3
Wow! It looks like they ARE the same. In other words, there was just some symbol/debug information contained inside still. I wonder what it could be. Well, they were compiled from different source filenames. Let's see if we use the same filename all the time, what happens.

Code:
$ cp 1.c test.c
$ gcc -o test1 test.c
$ cp 2.c test.c
$ gcc -o test2 test.c
$ cp 3.c test.c
$ gcc -o test3 test.c
$ rm test.c
$ md5sum test?
ea717e668c8ebc5159cba8eb411f975a  test1
ea717e668c8ebc5159cba8eb411f975a  test2
ea717e668c8ebc5159cba8eb411f975a  test3
Bingo. Must've been the filenames. And this is with NO optimization flags.

Conclusion: GCC's default settings on x86 produces identical code for the different versions. I would be interested in having someone using PPC or a similar architecture see if the case is the same on their systems. I may try on sparc later.
 
Old 07-30-2005, 05:00 PM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,500

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
checksumming the binary may lead you to wrong results (false negative comparison).

A simpler way would be to ask the compiler to produce an assembly file (-S), and then see what changes depending on the syntax or the optimization flags.
 
Old 07-31-2005, 11:26 AM   #11
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Some years ago I was surprised to find that the compiler optimised
Code:
if (somecondition)
	a = 1;
else
	a = 2;
better than
Code:
a = (somecondition) ? 1 : 2;
I'd have thought that the hint in the 2nd snippet that the same variable was being used would have tightened up the resultant code, but it was not to be. Disappointing, as I was trying to put forward a justification for the project mamagament not banning the use of the ?: construct because "it's confusing and not many people (ie them) understand it".
 
Old 07-31-2005, 11:56 AM   #12
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,485

Original Poster
Rep: Reputation: 66
lol wow this thread is still goin huh.


thanks for the output for assembly i dident know that yet
 
Old 08-01-2005, 04:41 PM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,500

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Quote:
Some years ago I was surprised to find that the compiler optimised
...
You may be confusing code generation and optimization.

While it may be true that assembly code generated from both of these examples differ unfavourably to the ternary operator, as soon as some optimization (-Osomething) is asked for, the compiler should generate identical code.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
X performance alaios Linux - Software 7 05-11-2004 09:54 AM
Need help with performance weng Linux - Newbie 11 01-22-2004 02:53 AM
Performance between RH 7.x and 9 lnuxwhat Linux - Software 0 06-27-2003 09:18 PM
hd performance crashmeister Linux - General 3 06-02-2002 07:00 AM
Performance ajtaus Linux - General 2 12-14-2001 05:57 PM


All times are GMT -5. The time now is 07:45 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration