Advertisement
obernardovieira

[speed test] Set&Get values (unordered_map vs char)

Oct 16th, 2013
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <string>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <iostream>
  7.  
  8. #include <chrono>
  9. long long timestamp()
  10. {
  11.     return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  12. }
  13.  
  14. char text_c[10000][128];
  15. typedef std::unordered_map<int, std::string> text_u;
  16.  
  17. int main() {
  18.     std::cout << "Inicio" << std::endl;
  19.     text_u Text_u;
  20.     int timea = timestamp();
  21.     for(int x=0; x!=10000; x++) {
  22.         strcpy(text_c[x],"Era uma vez...");
  23.     }
  24.     std::cout << "tempo " << timestamp()-timea << std::endl;
  25.    
  26.     int timeb = timestamp();
  27.     for(int x=0; x!=10000; x++) {
  28.         Text_u.insert(text_u::value_type(x,"Era uma vez..."));
  29.     }
  30.     std::cout << "tempo " << timestamp()-timeb << std::endl;
  31.    
  32.     char texto[128];
  33.     int time1 = timestamp();
  34.     for(int x=0; x!=10000; x++) {
  35.         strcpy(texto,text_c[x]);
  36.     }
  37.     std::cout << "tempo " << timestamp()-time1 << std::endl;
  38.    
  39.     int time2 = timestamp();
  40.     for(int x=0; x!=10000; x++) {
  41.         text_u::iterator j = Text_u.find(x);
  42.         strcpy(texto,(j->second).c_str());
  43.     }
  44.     std::cout << "tempo " << timestamp()-time2 << std::endl;
  45.    
  46.    
  47.     return 1;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement