Created
April 26, 2010 12:59
-
-
Save borman/379293 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <algorithm> | |
| #include <vector> | |
| #include <cstdlib> | |
| #include <cstdio> | |
| #include <time.h> | |
| #include <sys/types.h> | |
| using namespace std; | |
| struct SomeData | |
| { | |
| int a, b, c, d; | |
| }; | |
| extern bool less1(const SomeData &d1, const SomeData &d2) | |
| { | |
| return | |
| (d1.a<d2.a || | |
| (d1.a==d2.a && | |
| (d1.b<d2.b || | |
| (d1.b==d2.b && | |
| (d1.c<d2.c || | |
| (d1.c==d2.d && | |
| (d1.d<d2.d))))))); | |
| } | |
| extern bool less2(const SomeData &d1, const SomeData &d2) | |
| { | |
| if (d1.a<d2.a) | |
| return true; | |
| if (d1.a>d2.a) | |
| return false; | |
| if (d1.b<d2.b) | |
| return true; | |
| if (d1.b>d2.b) | |
| return false; | |
| if (d1.c<d2.c) | |
| return true; | |
| if (d1.c>d2.c) | |
| return false; | |
| if (d1.d<d2.d) | |
| return true; | |
| return false; | |
| } | |
| extern bool less3(const SomeData &d1, const SomeData &d2) | |
| { | |
| if (d1.a<d2.a) | |
| return true; | |
| if (d1.a==d2.a) | |
| { | |
| if (d1.b<d2.b) | |
| return true; | |
| if (d1.b==d2.b) | |
| { | |
| if (d1.c<d2.c) | |
| return true; | |
| if (d1.c==d2.c) | |
| { | |
| if (d1.d<d2.d) | |
| return true; | |
| } | |
| } | |
| } | |
| return false; | |
| } | |
| int main() | |
| { | |
| srand(1000); | |
| vector<SomeData> data1(10000000); | |
| for (int i=0; i<data1.size(); i++) | |
| { | |
| data1[i].a = rand(); | |
| data1[i].b = rand(); | |
| data1[i].c = rand(); | |
| data1[i].d = rand(); | |
| } | |
| vector<SomeData> data2 = data1, data3 = data1; | |
| clock_t clk = clock(); | |
| sort(data1.begin(), data1.end(), less1); | |
| unsigned int t1 = clock()-clk; | |
| clk = clock(); | |
| sort(data2.begin(), data2.end(), less2); | |
| unsigned int t2 = clock()-clk; | |
| clk = clock(); | |
| sort(data3.begin(), data3.end(), less3); | |
| unsigned int t3 = clock()-clk; | |
| printf("less1: %u\nless2: %u\nless3: %u\n", t1, t2, t3); | |
| return 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment