Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <chrono>
- #include <stdio.h>
- #include <stdlib.h>
- #include <xmemory>
- #include <typeinfo>
- #define in_ms(val)(std::chrono::duration<double, std::milli>(val).count())
- #pragma pack (push, 1)
- struct test_data
- {
- double a, b, c, d, e, f;
- test_data *lol;
- };
- #pragma pack (pop)
- template<typename T>
- class test_access
- {
- T value;
- public:
- T value_dot;
- T & getByReference()
- {
- return value;
- }
- const T & getByConstReference()
- {
- return value;
- }
- T * getByPointer()
- {
- return &value;
- }
- const T * getByConstPointer()
- {
- return &value;
- }
- T getByValue()
- {
- return value;
- }
- };
- typedef std::chrono::steady_clock::time_point ch_time;
- typedef std::chrono::high_resolution_clock hr_clock;
- bool mem_start = false;
- template<typename T>
- void test_malloc_vs_new(long trials, int size)
- {
- ch_time dt_tt;
- float dt_tt_ms = 0, dt_pt_ms = 0;
- if (!mem_start)
- {
- printf("New vs Malloc test started\n");
- printf("Iterations:%ld\nArray size:%d\n", trials, size);
- mem_start = true;
- }
- printf("Type:%s - %dBytes\n", typeid(T).name(), sizeof(T));
- printf("new - delete\n\n");
- dt_tt = hr_clock::now();
- for (long long I = 0; I < trials; ++I)
- {
- T *array = new T[size];
- memset(array, 0, size);
- delete[] array;
- }
- dt_tt_ms = in_ms(hr_clock::now() - dt_tt);
- dt_pt_ms = dt_tt_ms / trials;
- printf("TOTAL: %0.8fms\nPER TRIAL: %0.8fms\n", dt_tt_ms, dt_pt_ms);
- printf("\n\n");
- printf("malloc - free\n\n");
- dt_tt_ms = 0;
- dt_tt = hr_clock::now();
- dt_pt_ms = dt_tt_ms / trials;
- int bytes = sizeof(T)*size;
- for (long long I = 0; I < trials; ++I)
- {
- T *array = (T*)malloc(bytes);
- memset(array, 0, size);
- free(array);
- }
- dt_tt_ms = in_ms(hr_clock::now() - dt_tt);
- dt_pt_ms = dt_tt_ms / trials;
- printf("TOTAL: %0.8fms\nPER TRIAL: %0.8fms\n", dt_tt_ms, dt_pt_ms);
- }
- void test_class_access_value_method_pointer_dot(long long unsigned trials)
- {
- ch_time dt_tt;
- double dt_tt_ms = 0, dt_pt_ms = 0;
- test_data test, *test_p;
- test_p = new test_data();
- test_access<test_data> *test_ac = new test_access<test_data>();
- printf("class.value vs class.getValue() vs & class.getValue()\n");
- printf("Iterations:%d\n", trials);
- printf("Type:%s - %dBytes\n\n", typeid(test_data).name(), sizeof(test_data));
- printf("class.value\n");
- dt_tt_ms = 0;
- dt_tt = hr_clock::now();
- for (long long I = 0; I < trials; ++I)
- {
- test = test_ac->value_dot;
- }
- dt_tt_ms = in_ms(hr_clock::now() - dt_tt);
- dt_pt_ms = dt_tt_ms / trials;
- printf("TOTAL: %0.8fms\nPER TRIAL: %0.8fms\n", dt_tt_ms, dt_pt_ms);
- printf("%x\n", &test);
- printf("\n\n");
- printf("int class::getValue(){}\n");
- dt_tt_ms = 0;
- dt_tt = hr_clock::now();
- for (long long I = 0; I < trials; ++I)
- {
- test = test_ac->getByValue();
- }
- dt_tt_ms = in_ms(hr_clock::now() - dt_tt);
- dt_pt_ms = dt_tt_ms / trials;
- printf("TOTAL: %0.8fms\nPER TRIAL: %0.8fms\n", dt_tt_ms, dt_pt_ms);
- printf("%x\n", &test);
- printf("\n\n");
- printf("int & class::getValue(){}\n");
- dt_tt_ms = 0;
- dt_tt = hr_clock::now();
- for (long long I = 0; I < trials; ++I)
- {
- test = test_ac->getByReference();
- }
- dt_tt_ms = in_ms(hr_clock::now() - dt_tt);
- dt_pt_ms = dt_tt_ms / trials;
- printf("TOTAL: %0.8fms\nPER TRIAL: %0.8fms\n", dt_tt_ms, dt_pt_ms);
- printf("%x\n", &test);
- printf("\n\n");
- printf("const int & class::getValue(){}\n");
- dt_tt_ms = 0;
- dt_tt = hr_clock::now();
- for (long long I = 0; I < trials; ++I)
- {
- test = test_ac->getByConstReference();
- }
- dt_tt_ms = in_ms(hr_clock::now() - dt_tt);
- dt_pt_ms = dt_tt_ms / trials;
- printf("TOTAL: %0.8fms\nPER TRIAL: %0.8fms\n", dt_tt_ms, dt_pt_ms);
- printf("%x\n", &test);
- printf("\n\n");
- printf("int * class::getValue(){}\n");
- dt_tt_ms = 0;
- dt_tt = hr_clock::now();
- for (long long I = 0; I < trials; ++I)
- {
- test_ac->getByPointer()->a = 0.1;
- }
- dt_tt_ms = in_ms(hr_clock::now() - dt_tt);
- dt_pt_ms = dt_tt_ms / trials;
- printf("TOTAL: %0.8fms\nPER TRIAL: %0.8fms\n", dt_tt_ms, dt_pt_ms);
- printf("%x\n", &*test_p);
- printf("\n\n");
- printf("const int * class::getValue(){} const_cast<int *>\n");
- dt_tt_ms = 0;
- dt_tt = hr_clock::now();
- for (long long I = 0; I < trials; ++I)
- {
- test_p = const_cast<test_data *>(test_ac->getByConstPointer());
- }
- dt_tt_ms = in_ms(hr_clock::now() - dt_tt);
- dt_pt_ms = dt_tt_ms / trials;
- printf("TOTAL: %0.8fms\nPER TRIAL: %0.8fms\n", dt_tt_ms, dt_pt_ms);
- printf("%x\n", &*test_p);
- delete test_ac;
- }
- int main()
- {
- int iterations = 10000;
- test_malloc_vs_new<test_data>(iterations, 10000);
- printf("\n##################\n\n");
- test_malloc_vs_new<float>(iterations, 10000);
- printf("\n##################\n\n");
- test_malloc_vs_new<int>(iterations, 10000);
- printf("\n##################\n\n");
- test_malloc_vs_new<double>(iterations, 10000);
- printf("\n##################\n\n");
- test_malloc_vs_new<char>(iterations, 10000);
- printf("\n##################\n\n");
- test_malloc_vs_new<short>(iterations, 10000);
- printf("\n##################\n\n");
- test_class_access_value_method_pointer_dot(iterations);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement