lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] odd/vector-at-speed 72482da: Compare speed of vector


From: Greg Chicares
Subject: [lmi-commits] [lmi] odd/vector-at-speed 72482da: Compare speed of vector::at() to operator[]
Date: Mon, 30 Apr 2018 22:08:17 -0400 (EDT)

branch: odd/vector-at-speed
commit 72482da257855ddba47163941a135cb622775c7d
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Compare speed of vector::at() to operator[]
---
 objects.make     |  1 +
 sandbox_test.cpp | 31 +++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/objects.make b/objects.make
index 6fff9c3..7141d0f 100644
--- a/objects.make
+++ b/objects.make
@@ -907,6 +907,7 @@ safely_dereference_as_test$(EXEEXT): \
 sandbox_test$(EXEEXT): \
   $(common_test_objects) \
   sandbox_test.o \
+  timer.o \
 
 snprintf_test$(EXEEXT): \
   $(common_test_objects) \
diff --git a/sandbox_test.cpp b/sandbox_test.cpp
index 0ed05ae..b4d7b05 100644
--- a/sandbox_test.cpp
+++ b/sandbox_test.cpp
@@ -22,9 +22,40 @@
 #include "pchfile.hpp"
 
 #include "test_tools.hpp"
+#include "timer.hpp"
+
+#include <numeric>
+#include <vector>
 
 int test_main(int, char*[])
 {
+    std::vector<long long int>v(10'000'000);
+    std::iota(v.begin(), v.end(), 1);
+
+    {
+    Timer timer;
+    unsigned long long int sum = 0LL;
+    for(unsigned long long int j = 0; j < v.size(); ++j) sum += v.at(j);
+    std::cout << sum << " sum with at()\n"
+        << timer.stop().elapsed_msec_str() << std::endl;
+    }
+
+    {
+    Timer timer;
+    unsigned long long int sum = 0LL;
+    for(unsigned long long int j = 0; j < v.size(); ++j) sum += v[j];
+    std::cout << sum << " sum with []\n"
+        << timer.stop().elapsed_msec_str() << std::endl;
+    }
+
+    {
+    Timer timer;
+    unsigned long long int sum = 0LL;
+    for(auto const& i : v) sum += i;
+    std::cout << sum << " sum with ranged-for\n"
+        << timer.stop().elapsed_msec_str() << std::endl;
+    }
+
     return 0;
 }
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]