Компиляция: g++ -O3 1.cpp -felide-constructors -fno-exceptions -fno-rtti -funroll-loops -ffast-math -fomit-frame-pointer -march=opteron
Результаты:
test_map_inserts: Elapsed time: 1.900393s.
test_set_inserts: Elapsed time: 1.737445s.
test_vector_push_back: Elapsed time: 0.150905s.
test_vector_sort: Elapsed time: 0.105872s.
test_map_find: Elapsed time: 12.016727s.
test_set_find: Elapsed time: 11.943843s.
test_vector_find: Elapsed time: 5.747178s.
Mapsize =999752, Setsize=999778, Vsize=1000000
Код:
Copy Source | Copy HTML- #include <map>
- #include <algorithm>
- #include <stdio.h>
- #include <set>
- #include <stdlib.h>
- #include <string.h>
- #include <vector>
- #include <sys/stat.h>
- #include <sys/time.h>
-
-
- using namespace std;
-
- void start_profile();
- void end_profile_print();
-
-
- const long iterations = 10000000;
- const long inserts = 1000000;
- typedef long type1;
- typedef int type2;
-
- map<type1, type2> m;
- map<type1, type2>::iterator m_it;
- set<type1> s;
- set<type1>::iterator s_it;
- vector<type1> v;
- vector<type1>::iterator v_it;
-
- type1 my_rand(){
- return random();
- }
-
- void test_map_find(){
- type1 finder;
- for(long i=0; i<iterations; i++){
- finder = my_rand();
- m_it = m.find(finder);
- }
- }
-
- void test_set_find(){
- type1 finder;
- for(long i=0; i<iterations; i++){
- finder = my_rand();
- s_it = s.find(finder);
- }
- }
-
- void test_vector_find(){
- type1 finder;
- for(long i=0; i<iterations; i++){
- finder = my_rand();
- binary_search (v.begin(), v.end(), finder);
- }
- }
-
- void test_map_inserts(type2 x){
- type1 inserter;
- for(long i=0; i<inserts; i++){
- inserter = my_rand();
- m[inserter] = x;
- }
- }
- void test_set_inserts(){
- type1 inserter;
- for(long i=0; i<inserts; i++){
- inserter = my_rand();
- s.insert(inserter);
- }
- }
-
- void test_vector_push_back(){
- type1 inserter;
- for(long i=0; i<inserts; i++){
- inserter = my_rand();
- v.push_back(inserter);
- }
- }
-
- void test_vector_sort(){
- sort(v.begin(), v.end());
- }
-
- int main(){
- printf("test_map_inserts: ");
- start_profile();
- test_map_inserts(1);
- end_profile_print();
-
- printf("test_set_inserts: ");
- start_profile();
- test_set_inserts();
- end_profile_print();
-
- printf("test_vector_push_back: ");
- start_profile();
- test_vector_push_back();
- end_profile_print();
-
- printf("test_vector_sort: ");
- start_profile();
- test_vector_sort();
- end_profile_print();
-
- printf("test_map_find: ");
- start_profile();
- test_map_find();
- end_profile_print();
-
- printf("test_set_find: ");
- start_profile();
- test_set_find();
- end_profile_print();
-
- printf("test_vector_find: ");
- start_profile();
- test_vector_find();
- end_profile_print();
-
-
- printf("Mapsize =%ld, Setsize=%ld, Vsize=%ld\n", (long)m.size(), (long)s.size(), (long)v.size());
- return 0;
- }
-
- static struct timeval t1,t2;
-
- void start_profile(){
- gettimeofday(&t1, NULL);
- }
- void end_profile_print(){
- gettimeofday(&t2, NULL);
- double d1 = double(t1.tv_sec) + double(t1.tv_usec ) / 1000000.0;
- double d2 = double(t2.tv_sec) + double(t2.tv_usec ) / 1000000.0;
- printf("Elapsed time: %fs.\n", d2-d1);
- }
-
October 8 2009, 15:13:01 UTC 2 years ago
April 7 2011, 17:13:15 UTC 1 year ago
http://habrahabr.ru/blogs/cpp/11451