🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.linux.misc
Article #54

Re: Two things, RedHat + Linux efficiency

#54
From: Nick Kralevich
Date: Wed, 13 Mar 1996 05:11
94 lines
3932 bytes
In article <4lFXuqq00aw=M=iUNp@andrew.cmu.edu>,
Nicholas P Konidaris  <npk+@andrew.cmu.edu> wrote:
>Ok, these are two totally unrelated questions/comments..  I was coding
>around on my box, and wanted to see the speed difference (my CS prof
>told me that x++ is faster than x = x + 1)  So, I write code that looks
>like:

The appropriate place for this is comp.lang.c, but oh well.  I've 
added comp.lang.c to the newsgroup line.

On some instruction sets, there is actually an "incriment" instruction
built into the hardware.  I'm not sure if this is true with the
intel platform.  If this is the case, then the one incriment instruction
would be faster than having to do a load, add, and store (3 instructions).

>......
>for (cnt = 0; cnt < 50000; cnt++)
>	x = x + 1
>......
>and
>......
>for (cnt = 0; cnt < 50000; cnt++)
>	x++
>......

However, in the case here, it shouldn't make a difference.  That is
because you only need 1 load, 1 store for the outside of the loop,
then the rest of the iterations could be done in registers.

Note 1:  There's a bug in your program.  "x" isn't properly initialized. :)

Note 2:  If you were to run "gcc -O2 blah...", that should reduce the
loop to:

    cnt = 50000;
    x = 50000;

Since the value of x and cnt can be computed at compile time.

>I use the  good 'ol 'time' command, and get pretty much the exact same
>time to do both those computations.  Now, I substitute the x++ with x--
>and for some really odd reason, the x-- is slower.... Is this a gcc
>thing, or a linux thing or what :)  Any pointers/suggestions of why this
>happens would be appreciated :)

My understanding is that there should be no speed difference between
x++ and x--.  (Of course, I'm not an expert on the intel platform, so
someone should correct me if I'm wrong).

Here's a couple of things that could have gone wrong with your testing:

50000 loops is too few to time with any accuracy.  The overhead
of loading in shared libraries, initializing the program, scheduling
problems, caching, etc...  all would have degraded the accuracy of your 
timing measurements.  Ideally, you would compile your program with:

   gcc -O2 -fomit-frame-pointer -static timecount.c -o timecount

to get good numbers.  The "-static" is the most important, because it 
eliminates the overhead of bringing in shared libraries, at the expense
of making your executable size bigger.

Also, you probibly want to put the timing code in your code, instead
of relying on an external program ("time") to time your program.  
So your program would look somthing like:

main()
{
  double seconds1,seconds2;
  int count,i;
  seconds1 = get_seconds();
  i = 0;
  for(count = 0; count < 50000; count++) {
    i++;
  }
  seconds2 = get_seconds();
  printf("Total time = %f\n", seconds2 - seconds1);
}

Where "get_seconds()" is a high performance timer call.

Anyway, I think I've gone off the deep end here, and I don't know
if I really answered your question or not.  There shouldn't
be any difference in speed between "x++", "x--", and "x = x + 1" on
modern computers.  Only computers that have an incriment or decriment
instruction might show improvements, but even that I'm not
sure about.

Perhaps someone has an intel instruction guide listing the available
instructions, and the time they take to execute.

Take care,
-- Nick Kralevich
   nickkral@cory.eecs.berkeley.edu
Technical Details
Message-ID: <4i5lev$fd3@agate.berkeley.edu>
Path: arc1-ca.pugleaf.net!news.duke.edu!agate!nickkral
References: <4lFXuqq00aw=M=iUNp@andrew.cmu.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.duke.edu!agate!nickkral
From: nickkral@america.CS.Berkeley.EDU (Nick Kralevich)
Newsgroups: comp.os.linux.misc,comp.lang.c
Subject: Re: Two things, RedHat + Linux efficiency
Date: 13 Mar 1996 05:11:59 GMT
Organization: Electrical Engineering Computer Science Department, University of California at Berkeley
Message-ID: <4i5lev$fd3@agate.berkeley.edu>
References: <4lFXuqq00aw=M=iUNp@andrew.cmu.edu>
NNTP-Posting-Host: america.cs.berkeley.edu