Advertisement
AI_UBI

C++ Test [new vs malloc] and few methods of access to class

Apr 19th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.16 KB | None | 0 0
  1. #include <chrono>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <xmemory>
  5. #include <typeinfo>
  6.  
  7. #define in_ms(val)(std::chrono::duration<double, std::milli>(val).count())
  8. #pragma pack (push, 1)
  9. struct test_data
  10. {
  11.     double a, b, c, d, e, f;
  12.     test_data *lol;
  13. };
  14. #pragma pack (pop)
  15. template<typename T>
  16. class test_access
  17. {
  18.     T value;
  19.  
  20. public:
  21.     T value_dot;
  22.  
  23.     T & getByReference()
  24.     {
  25.         return value;
  26.     }
  27.  
  28.     const T & getByConstReference()
  29.     {
  30.         return value;
  31.     }
  32.  
  33.     T * getByPointer()
  34.     {
  35.         return &value;
  36.     }
  37.  
  38.     const T * getByConstPointer()
  39.     {
  40.         return &value;
  41.     }
  42.  
  43.     T getByValue()
  44.     {
  45.         return value;
  46.     }
  47. };
  48.  
  49. typedef std::chrono::steady_clock::time_point ch_time;
  50. typedef std::chrono::high_resolution_clock hr_clock;
  51. bool mem_start = false;
  52. template<typename T>
  53. void test_malloc_vs_new(long trials, int size)
  54. {
  55.     ch_time dt_tt;
  56.     float dt_tt_ms = 0, dt_pt_ms = 0;
  57.  
  58.     if (!mem_start)
  59.     {
  60.         printf("New vs Malloc test started\n");
  61.         printf("Iterations:%ld\nArray size:%d\n", trials, size);
  62.         mem_start = true;
  63.     }
  64.  
  65.     printf("Type:%s - %dBytes\n", typeid(T).name(), sizeof(T));
  66.     printf("new - delete\n\n");
  67.  
  68.     dt_tt = hr_clock::now();
  69.  
  70.     for (long long I = 0; I < trials; ++I)
  71.     {
  72.         T *array = new T[size];
  73.         memset(array, 0, size);
  74.         delete[] array;
  75.     }
  76.  
  77.     dt_tt_ms = in_ms(hr_clock::now() - dt_tt);
  78.     dt_pt_ms = dt_tt_ms / trials;
  79.  
  80.     printf("TOTAL: %0.8fms\nPER TRIAL: %0.8fms\n", dt_tt_ms, dt_pt_ms);
  81.  
  82.     printf("\n\n");
  83.  
  84.     printf("malloc - free\n\n");
  85.  
  86.     dt_tt_ms = 0;
  87.     dt_tt = hr_clock::now();
  88.     dt_pt_ms = dt_tt_ms / trials;
  89.  
  90.     int bytes = sizeof(T)*size;
  91.  
  92.     for (long long I = 0; I < trials; ++I)
  93.     {
  94.         T *array = (T*)malloc(bytes);
  95.         memset(array, 0, size);
  96.         free(array);
  97.     }
  98.    
  99.     dt_tt_ms = in_ms(hr_clock::now() - dt_tt);
  100.     dt_pt_ms = dt_tt_ms / trials;
  101.  
  102.     printf("TOTAL: %0.8fms\nPER TRIAL: %0.8fms\n", dt_tt_ms, dt_pt_ms);
  103. }
  104.  
  105.  
  106. void test_class_access_value_method_pointer_dot(long long unsigned trials)
  107. {
  108.     ch_time dt_tt;
  109.     double dt_tt_ms = 0, dt_pt_ms = 0;
  110.  
  111.     test_data test, *test_p;
  112.  
  113.     test_p = new test_data();
  114.     test_access<test_data> *test_ac = new test_access<test_data>();
  115.  
  116.     printf("class.value vs class.getValue() vs & class.getValue()\n");
  117.     printf("Iterations:%d\n", trials);
  118.     printf("Type:%s - %dBytes\n\n", typeid(test_data).name(), sizeof(test_data));
  119.     printf("class.value\n");
  120.  
  121.     dt_tt_ms = 0;
  122.     dt_tt = hr_clock::now();
  123.  
  124.     for (long long I = 0; I < trials; ++I)
  125.     {
  126.         test = test_ac->value_dot;
  127.     }
  128.    
  129.     dt_tt_ms = in_ms(hr_clock::now() - dt_tt);
  130.     dt_pt_ms = dt_tt_ms / trials;
  131.    
  132.     printf("TOTAL: %0.8fms\nPER TRIAL: %0.8fms\n", dt_tt_ms, dt_pt_ms);
  133.     printf("%x\n", &test);
  134.     printf("\n\n");
  135.  
  136.     printf("int class::getValue(){}\n");
  137.  
  138.     dt_tt_ms = 0;
  139.     dt_tt = hr_clock::now();
  140.  
  141.     for (long long I = 0; I < trials; ++I)
  142.     {
  143.         test = test_ac->getByValue();
  144.     }
  145.    
  146.     dt_tt_ms = in_ms(hr_clock::now() - dt_tt);
  147.     dt_pt_ms = dt_tt_ms / trials;
  148.    
  149.     printf("TOTAL: %0.8fms\nPER TRIAL: %0.8fms\n", dt_tt_ms, dt_pt_ms);
  150.     printf("%x\n", &test);
  151.     printf("\n\n");
  152.  
  153.     printf("int & class::getValue(){}\n");
  154.  
  155.     dt_tt_ms = 0;
  156.     dt_tt = hr_clock::now();
  157.  
  158.     for (long long I = 0; I < trials; ++I)
  159.     {
  160.         test = test_ac->getByReference();
  161.     }
  162.    
  163.     dt_tt_ms = in_ms(hr_clock::now() - dt_tt);
  164.     dt_pt_ms = dt_tt_ms / trials;
  165.    
  166.     printf("TOTAL: %0.8fms\nPER TRIAL: %0.8fms\n", dt_tt_ms, dt_pt_ms);
  167.     printf("%x\n", &test);
  168.     printf("\n\n");
  169.  
  170.     printf("const int & class::getValue(){}\n");
  171.  
  172.     dt_tt_ms = 0;
  173.     dt_tt = hr_clock::now();
  174.  
  175.     for (long long I = 0; I < trials; ++I)
  176.     {
  177.         test = test_ac->getByConstReference();
  178.     }
  179.    
  180.     dt_tt_ms = in_ms(hr_clock::now() - dt_tt);
  181.     dt_pt_ms = dt_tt_ms / trials;
  182.    
  183.     printf("TOTAL: %0.8fms\nPER TRIAL: %0.8fms\n", dt_tt_ms, dt_pt_ms);
  184.     printf("%x\n", &test);
  185.     printf("\n\n");
  186.  
  187.     printf("int * class::getValue(){}\n");
  188.  
  189.     dt_tt_ms = 0;
  190.     dt_tt = hr_clock::now();
  191.  
  192.     for (long long I = 0; I < trials; ++I)
  193.     {
  194.         test_ac->getByPointer()->a = 0.1;
  195.     }
  196.    
  197.     dt_tt_ms = in_ms(hr_clock::now() - dt_tt);
  198.     dt_pt_ms = dt_tt_ms / trials;
  199.    
  200.     printf("TOTAL: %0.8fms\nPER TRIAL: %0.8fms\n", dt_tt_ms, dt_pt_ms);
  201.     printf("%x\n", &*test_p);
  202.     printf("\n\n");
  203.  
  204.     printf("const int * class::getValue(){} const_cast<int *>\n");
  205.  
  206.     dt_tt_ms = 0;
  207.     dt_tt = hr_clock::now();
  208.  
  209.     for (long long I = 0; I < trials; ++I)
  210.     {
  211.         test_p = const_cast<test_data *>(test_ac->getByConstPointer());
  212.     }
  213.    
  214.     dt_tt_ms = in_ms(hr_clock::now() - dt_tt);
  215.     dt_pt_ms = dt_tt_ms / trials;
  216.    
  217.     printf("TOTAL: %0.8fms\nPER TRIAL: %0.8fms\n", dt_tt_ms, dt_pt_ms);
  218.     printf("%x\n", &*test_p);
  219.    
  220.     delete test_ac;
  221.    
  222. }
  223.  
  224.  
  225. int main()
  226. {
  227.     int iterations = 10000;
  228.  
  229.     test_malloc_vs_new<test_data>(iterations, 10000);
  230.     printf("\n##################\n\n");
  231.     test_malloc_vs_new<float>(iterations, 10000);
  232.     printf("\n##################\n\n");
  233.     test_malloc_vs_new<int>(iterations, 10000);
  234.     printf("\n##################\n\n");
  235.     test_malloc_vs_new<double>(iterations, 10000);
  236.     printf("\n##################\n\n");
  237.     test_malloc_vs_new<char>(iterations, 10000);
  238.     printf("\n##################\n\n");
  239.     test_malloc_vs_new<short>(iterations, 10000);
  240.     printf("\n##################\n\n");
  241.     test_class_access_value_method_pointer_dot(iterations);
  242.  
  243.     getchar();
  244.  
  245.  
  246.     return 0;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement