++var VS var++

From: Computer Whizz (old486whizz_at_hotmail.com)
Date: 01/06/05


Date: Thu, 6 Jan 2005 03:18:19 -0000

Hey all,

I was just doing some minor investigations into unsigned long int's after
someone posted up a post about the fibonacci (spelling?) sequence.

Anyway, I decided to do some time tests. The code is:

#include <iostream>
#include <ostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main () {

 string testy;
 time_t start = time(NULL), end;
 unsigned long int current=0, last=0;
 for (; current!=50000; last = ++current) {
  cout << "test = " << current << "\n";
 }

 end = time(NULL);
 cout << "time = " << (end - start) << "\n" << last << endl;
 system("PAUSE");
 return 0;
}

I'm sure it's not the best, but I was curious. I read in Accelerated C++
that ++var is faster than var++ as it just adds one and returns the value,
while var++, adds one and returns the previous variable (meaning it creates
another variable to store this value).

The above code generates 17 seconds if the compiler is using optimising and
18 when it isn't. BUT using current++ instead gives me 16 seconds optimised
or not.

As I previously said, I was experimenting with the fibonacci sequence, so I
wanted to leave the "last =" in there...

Can anyone shed some light on the subject?

-- 
=========
Comp Whizz
=========
(The C++ beginner) 


Relevant Pages

  • N[(1/2)-k*(k-1/2)*(-3)/Factorial[k], 10000]
    ... in S_n.%C A000045 This is also the Horadam sequence.-Ross La ... %C A000045 The Fibonacci sequence,like any additive sequence,naturally ... representation for Lucas numbers.Many Fibonacci formulas ... Fibonacci Numbers%H A000045 Eric Weisstein's World of Mathematics, ...
    (sci.math)
  • Re: c language
    ... this sequence was described by the Indian mathematicians Gopala and Hemachandra in 1150, who were investigating the possible ways of exactly bin packing items of length 1 and 2. ... In the West, it was first studied by Leonardo of Pisa, who was also known as Fibonacci, to describe the growth of an idealised rabbit population. ... Every positive integer can be written in a unique way as the sum of one or more distinct Fibonacci numbers in such a way that the sum does not include any two consecutive Fibonacci numbers. ... For example, the solutions to reaction-diffusion differential equations can show such a patterning; in biology, genes often express themselves through gene regulatory networks, that is, in terms of several enzymes controlling a reaction, which can be modelled with reaction-diffusion equations. ...
    (comp.programming)
  • Re: Whats with this sequence?
    ... number and the corresponding Fibonacci number. ... Starting with the first triangle number and the first ... Gives this resulting sequence -- ... difference table is all zeroes from its 3rd row onward. ...
    (sci.math)
  • Re: 1/89 and the Fibonacci sequence-
    ... You will see the Fibonacci numbers being generated when you develop the power series, and you will see them to fall into place when you multiply out the identity ... All power series developments of reciprocals of polynomials yield recurrent relations for the coefficients. ... >>Then adding the Fibonacci sequence in this manner -- ... >>Will this continue repeating the period of 1/89>no matter how many fibonacci numbers are added>in this manner? ...
    (sci.math)
  • Re: ++var VS var++
    ... | that ++var is faster than var++ as it just adds one and returns the value, ... | As I previously said, I was experimenting with the fibonacci sequence, so I ... class Timer ... Postfix Operator: ...
    (alt.comp.lang.learn.c-cpp)