Advertisement
Guest User

C++ Array vs Vector Benchmark

a guest
Jul 21st, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. using Nuclex::Game::Timing::Clock;
  2. using Nuclex::Support::Text::lexical_cast;
  3. std::shared_ptr<Clock> clock(Clock::Create());
  4.  
  5. std::size_t testSize = 100000000;
  6. if(commandLine == nullptr) {
  7.   testSize += 1234; // fool the optimizer
  8. }
  9.  
  10. // ---
  11. std::uint64_t arrayStart = clock->GetTime();
  12. {
  13.   int *numbers2 = new int[testSize];
  14.   for(std::size_t index = 0; index < testSize; ++index) {
  15.     numbers2[index] = static_cast<int>(index);
  16.   }
  17.   delete[] numbers2;
  18. }
  19. std::uint64_t arrayEnd = clock->GetTime();
  20.  
  21. // ---
  22. std::uint64_t vectorStart = clock->GetTime();
  23. {
  24.   std::vector<int> numbers(testSize);
  25.   for(std::size_t index = 0; index < numbers.size(); ++index) {
  26.     numbers[index] = static_cast<int>(index);
  27.   }
  28. }
  29. std::uint64_t vectorEnd = clock->GetTime();
  30.  
  31.  
  32. {
  33.   std::uint64_t arrayDuration = arrayEnd - arrayStart;
  34.   arrayDuration /= (clock->GetFrequency() / 1000);
  35.   std::string message =
  36.     std::string("Array duration: ") +
  37.     lexical_cast<std::string>(arrayDuration) +
  38.     std::string(" ms");
  39.   OutputDebugStringA(message.c_str());
  40. }
  41.  
  42. {
  43.   std::uint64_t vectorDuration = vectorEnd - vectorStart;
  44.   vectorDuration /= (clock->GetFrequency() / 1000);
  45.   std::string message =
  46.     std::string("Vector duration: ") +
  47.     lexical_cast<std::string>(vectorDuration) +
  48.     std::string(" ms");
  49.   OutputDebugStringA(message.c_str());
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement