[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master cbb6cad 4/4: Measure speed of various stream-
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master cbb6cad 4/4: Measure speed of various stream-cast implementations |
Date: |
Wed, 16 Jan 2019 15:24:18 -0500 (EST) |
branch: master
commit cbb6cade2f89a3bada06860f5179c4695662b137
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Measure speed of various stream-cast implementations
The purpose of the added tests is to measure various speed improvements,
and ultimately to make stream_cast<>() and ledger_format() faster. See:
https://lists.nongnu.org/archive/html/lmi/2019-01/msg00005.html
et seqq.
---
Makefile.am | 3 +-
objects.make | 1 +
stream_cast_test.cpp | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 113 insertions(+), 1 deletion(-)
diff --git a/Makefile.am b/Makefile.am
index c4909cc..4ee10cb 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1075,7 +1075,8 @@ test_stratified_algorithms_CXXFLAGS = $(AM_CXXFLAGS)
test_stream_cast_SOURCES = \
$(common_test_objects) \
facets.cpp \
- stream_cast_test.cpp
+ stream_cast_test.cpp \
+ timer.cpp
test_stream_cast_CXXFLAGS = $(AM_CXXFLAGS)
test_system_command_SOURCES = \
diff --git a/objects.make b/objects.make
index a45395b..c1c89d5 100644
--- a/objects.make
+++ b/objects.make
@@ -949,6 +949,7 @@ stream_cast_test$(EXEEXT): \
$(common_test_objects) \
facets.o \
stream_cast_test.o \
+ timer.o \
system_command_test$(EXEEXT): \
$(common_test_objects) \
diff --git a/stream_cast_test.cpp b/stream_cast_test.cpp
index 720f3e5..d391ed6 100644
--- a/stream_cast_test.cpp
+++ b/stream_cast_test.cpp
@@ -24,6 +24,114 @@
#include "stream_cast.hpp"
#include "test_tools.hpp"
+#include "timer.hpp"
+
+/// A minimalistic clone of stream_cast<>().
+
+template<typename To, typename From>
+To cast_1(From from, To = To())
+{
+ std::stringstream interpreter;
+ interpreter.imbue(blank_is_not_whitespace_locale());
+
+ To result = To();
+ if
+ ( !(interpreter << from)
+ || !(interpreter >> result)
+ || !(interpreter >> std::ws).eof()
+ )
+ {
+ throw std::runtime_error("Oops!");
+ }
+ return result;
+}
+
+/// Like cast_1<>(), but with static 'interpreter'.
+
+template<typename To, typename From>
+To cast_2(From from, To = To())
+{
+ static std::stringstream interpreter;
+ interpreter.imbue(blank_is_not_whitespace_locale());
+ interpreter.str(std::string{});
+ interpreter.clear();
+
+ To result = To();
+ if
+ ( !(interpreter << from)
+ || !(interpreter >> result)
+ || !(interpreter >> std::ws).eof()
+ )
+ {
+ throw std::runtime_error("Oops!");
+ }
+ return result;
+}
+
+std::stringstream& imbued()
+{
+ static std::stringstream interpreter;
+ interpreter.imbue(blank_is_not_whitespace_locale());
+ return interpreter;
+}
+
+/// Like cast_2<>(), but with statically imbued static 'interpreter'.
+
+template<typename To, typename From>
+To cast_3(From from, To = To())
+{
+ std::stringstream& interpreter {imbued()};
+ interpreter.str(std::string{});
+ interpreter.clear();
+ To result = To();
+ if
+ ( !(interpreter << from)
+ || !(interpreter >> result)
+ || !(interpreter >> std::ws).eof()
+ )
+ {
+ throw std::runtime_error("Oops!");
+ }
+ return result;
+}
+
+/// Like cast_3<>(), but without calling str().
+
+template<typename To, typename From>
+To cast_4(From from, To = To())
+{
+ std::stringstream& interpreter {imbued()};
+ interpreter.clear();
+ To result = To();
+ if
+ ( !(interpreter << from)
+ || !(interpreter >> result)
+ || !(interpreter >> std::ws).eof()
+ )
+ {
+ throw std::runtime_error("Oops!");
+ }
+ return result;
+}
+
+void assay_speed()
+{
+ static double const e {2.718281828459045};
+ auto f0 = [] {stream_cast<std::string>(e);};
+ auto f1 = [] {cast_1 <std::string>(e);};
+ auto f2 = [] {cast_2 <std::string>(e);};
+ auto f3 = [] {cast_3 <std::string>(e);};
+ auto f4 = [] {cast_4 <std::string>(e);};
+ std::cout
+ << "\n Speed tests..."
+ << "\n stream_cast : " << TimeAnAliquot(f0)
+ << "\n minimalistic : " << TimeAnAliquot(f1)
+ << "\n static stream : " << TimeAnAliquot(f2)
+ << "\n static facet too: " << TimeAnAliquot(f3)
+ << "\n without str() : " << TimeAnAliquot(f4)
+ << std::endl
+ ;
+}
int test_main(int, char*[])
{
@@ -98,6 +206,8 @@ int test_main(int, char*[])
,"Cannot convert (char const*)(0) to std::string."
);
+ assay_speed();
+
return 0;
}