Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <random>
- #include <chrono>
- #include <unistd.h>
- #include <functional>
- int main(void) {
- const int n{15000};
- int* p{new int[n]};
- int * ep{ p + n };
- auto rand2 = std::bind(std::uniform_int_distribution<>(7,102), std::mt19937(std::random_device()()));
- for(int * q{p}; q!=ep; ++q )
- {
- *q = rand2();
- }
- for (int j{0}; j < 20; j++) {
- {
- auto start_pointer = std::chrono::high_resolution_clock::now();
- for (int* q{p}; q != ep; ++q) {
- *q +=1;
- }
- auto end_pointer = std::chrono::high_resolution_clock::now();
- auto time_pointer = std::chrono::duration_cast<std::chrono::nanoseconds>(
- end_pointer - start_pointer)
- .count();
- std::cout << " Pointer: " << time_pointer << std::endl;
- }
- {
- auto start_pointer = std::chrono::high_resolution_clock::now();
- for (int* q{p}; q != ep; ++q) {
- *q += 1;
- }
- auto end_pointer = std::chrono::high_resolution_clock::now();
- auto time_pointer = std::chrono::duration_cast<std::chrono::nanoseconds>(
- end_pointer - start_pointer)
- .count();
- std::cout << " Pointer: " << time_pointer << std::endl;
- }
- {
- auto start_index_32 = std::chrono::high_resolution_clock::now();
- for (int i{0}; i != n; i++) {
- p[i] += 1;
- }
- auto end_index_32 = std::chrono::high_resolution_clock::now();
- auto time_index_32 = std::chrono::duration_cast<std::chrono::nanoseconds>(
- end_index_32 - start_index_32)
- .count();
- std::cout << "Index 32: " << time_index_32 << std::endl;
- }
- {
- auto start_index_32 = std::chrono::high_resolution_clock::now();
- for (int i{0}; i != n; i++) {
- p[i] += 1;
- }
- auto end_index_32 = std::chrono::high_resolution_clock::now();
- auto time_index_32 = std::chrono::duration_cast<std::chrono::nanoseconds>(
- end_index_32 - start_index_32)
- .count();
- std::cout << "Index 32: " << time_index_32 << std::endl;
- }
- {
- const std::size_t n_64{n};
- auto start_index_64 = std::chrono::high_resolution_clock::now();
- for (std::size_t i{0}; i != n_64; i++) {
- p[i] += 1;
- }
- auto end_index_64 = std::chrono::high_resolution_clock::now();
- auto time_index_64 = std::chrono::duration_cast<std::chrono::nanoseconds>(
- end_index_64 - start_index_64)
- .count();
- std::cout << "Index 64: " << time_index_64 << std::endl;
- }
- {
- const std::size_t n_64{n};
- auto start_index_64 = std::chrono::high_resolution_clock::now();
- for (std::size_t i{0}; i != n_64; i++) {
- p[i] += 1;
- }
- auto end_index_64 = std::chrono::high_resolution_clock::now();
- auto time_index_64 = std::chrono::duration_cast<std::chrono::nanoseconds>(
- end_index_64 - start_index_64)
- .count();
- std::cout << "Index 64: " << time_index_64 << std::endl;
- }
- std::cout << std::endl;
- }
- delete[] p;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement