++var VS var++
From: Computer Whizz (old486whizz_at_hotmail.com)
Date: 01/06/05
- Next message: Computer Whizz: "Re: Read Directory"
- Previous message: Chris \( Val \): "Re: Read Directory"
- Next in thread: Chris \( Val \): "Re: ++var VS var++"
- Reply: Chris \( Val \): "Re: ++var VS var++"
- Reply: Edd: "Re: ++var VS var++"
- Reply: Andrey Tarasevich: "Re: ++var VS var++"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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)
- Next message: Computer Whizz: "Re: Read Directory"
- Previous message: Chris \( Val \): "Re: Read Directory"
- Next in thread: Chris \( Val \): "Re: ++var VS var++"
- Reply: Chris \( Val \): "Re: ++var VS var++"
- Reply: Edd: "Re: ++var VS var++"
- Reply: Andrey Tarasevich: "Re: ++var VS var++"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|