-
Code's Tags
-
Your Codes
-
Reffers
-
Linked Codes
|
Code:
Short link for Twitter:
HTML:
HTML view:
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);
- }
-
|