Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sys/time.h> // for gettimeofday()
- // g++ main.cpp -o swap_test -march=native -O2
- using namespace std;
- #define NUMSTEPS 1000000000
- int main(){
- timeval t1, t2, t3, t4;
- unsigned int a = 32, b = 64, c = 0, i = 0;
- double elapsedTime;
- gettimeofday(&t1, NULL);
- for(i = 0; i < NUMSTEPS; ++i){
- swap(a, b);
- }
- gettimeofday(&t2, NULL);
- for(i = 0; i < NUMSTEPS; ++i){
- a^=b;
- b^=a;
- a^=b;
- }
- gettimeofday(&t3, NULL);
- for(i = 0; i < NUMSTEPS; ++i){
- c = b;
- b = a;
- a = c;
- }
- gettimeofday(&t4, NULL);
- // compute and print the elapsed time in millisec
- elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; // sec to ms
- elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0; // us to ms
- cout << "swap\t" << elapsedTime << " ms.\n";
- elapsedTime = (t3.tv_sec - t2.tv_sec) * 1000.0; // sec to ms
- elapsedTime += (t3.tv_usec - t2.tv_usec) / 1000.0; // us to ms
- cout << "XOR\t" << elapsedTime << " ms.\n";
- elapsedTime = (t4.tv_sec - t3.tv_sec) * 1000.0; // sec to ms
- elapsedTime += (t4.tv_usec - t3.tv_usec) / 1000.0; // us to ms
- cout << "+var\t" << elapsedTime << " ms.\n";
- cout<<a<<b<<c<<"\b\b\b\b\b\b";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement