lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 2a3d961 03/13: Measure cost of a needless tra


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 2a3d961 03/13: Measure cost of a needless transcendental calculation
Date: Fri, 9 Apr 2021 18:42:36 -0400 (EDT)

branch: master
commit 2a3d961046af842d4373db60535c7c2d7812893d
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Measure cost of a needless transcendental calculation
---
 math_functions_test.cpp | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/math_functions_test.cpp b/math_functions_test.cpp
index 1267823..d42b268 100644
--- a/math_functions_test.cpp
+++ b/math_functions_test.cpp
@@ -26,6 +26,7 @@
 #include "fenv_lmi.hpp"
 #include "materially_equal.hpp"
 #include "miscellany.hpp"               // stifle_warning_for_unused_value()
+#include "stl_extensions.hpp"           // nonstd::power()
 #include "test_tools.hpp"
 #include "timer.hpp"
 
@@ -191,7 +192,7 @@ void sample_results()
     fenv_initialize();
 }
 
-// These 'meteN' functions perform the same set of operations using
+// These 'mete[0123]' functions perform the same set of operations using
 // different implementations.
 
 // This implementation naively uses std::pow(); it is both slower and
@@ -243,6 +244,37 @@ void mete3()
         }
 }
 
+// These 'mete[45]' functions calculate 10^-9 in different ways.
+// The SGI extension is about eight times as fast as calling a
+// transcendental function; that outcome is not surprising, but
+// quantifying it is useful. Of course, it would not be surprising
+// to find that a table lookup would be even faster for "reasonable"
+// powers of ten.
+
+void mete4()
+{
+    double volatile base = 10.0;
+    int    volatile exp  = 9;
+    double volatile x;
+    stifle_warning_for_unused_value(x);
+    for(int j = 0; j < 100000; ++j)
+        {
+        x = 1.0 / nonstd::power(base, exp);
+        }
+}
+
+void mete5()
+{
+    double volatile base = 10.0;
+    int    volatile exp  = -9;
+    double volatile x;
+    stifle_warning_for_unused_value(x);
+    for(int j = 0; j < 100000; ++j)
+        {
+        x = std::pow(base, exp);
+        }
+}
+
 void assay_speed()
 {
     std::cout << "Speed tests:\n";
@@ -250,6 +282,8 @@ void assay_speed()
     std::cout << "  std::expm1       " << TimeAnAliquot(mete1) << '\n';
     std::cout << "  double      i365 " << TimeAnAliquot(mete2) << '\n';
     std::cout << "  long double i365 " << TimeAnAliquot(mete3) << '\n';
+    std::cout << "  10^-9 nonstd     " << TimeAnAliquot(mete4) << '\n';
+    std::cout << "  10^-9 std        " << TimeAnAliquot(mete5) << '\n';
 }
 
 int test_main(int, char*[])



reply via email to

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