[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master de40a50 4/4: Simplify simplify_floating_point
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master de40a50 4/4: Simplify simplify_floating_point() |
Date: |
Mon, 26 Feb 2018 18:50:51 -0500 (EST) |
branch: master
commit de40a50a80642f735888bd957d6ed2fa4b9c059d
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Simplify simplify_floating_point()
The decision tree here:
http://lists.nongnu.org/archive/html/lmi/2018-02/msg00157.html
selects this more scrutable algorithm based on performance measurements
using the unit test:
new algorithm, then old: median of five runs
2/3, lmi : 4.042e-006 s mean; 4 us least of 2474 runs
2/3, lmi : 3.983e-006 s mean; 4 us least of 2511 runs
new algorithm, then old: fastest of five runs
2/3, lmi : 3.758e-006 s mean; 4 us least of 2661 runs
2/3, lmi : 3.539e-006 s mean; 3 us least of 2826 runs
That comparison measures mete_two_thirds(), which converts from double
to string and back again: it doesn't measure simplify_floating_point()
in isolation, but it demonstrates that there's no gross pessimization
that would override the desire for clarity.
---
Makefile.am | 10 +++++++++-
numeric_io_traits.hpp | 14 +++++---------
objects.make | 8 ++++++++
3 files changed, 22 insertions(+), 10 deletions(-)
diff --git a/Makefile.am b/Makefile.am
index 07f475d..2d2f3d5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -569,7 +569,8 @@ test_alert_CXXFLAGS = $(AM_CXXFLAGS)
test_any_member_SOURCES = \
$(common_test_objects) \
any_member_test.cpp \
- facets.cpp
+ facets.cpp \
+ miscellany.cpp
test_any_member_CXXFLAGS = $(AM_CXXFLAGS)
test_assert_lmi_SOURCES = \
@@ -723,6 +724,7 @@ test_gpt_SOURCES = \
gpt_commutation_functions.cpp \
gpt_test.cpp \
ihs_irc7702.cpp \
+ miscellany.cpp \
timer.cpp
test_gpt_CXXFLAGS = $(AM_CXXFLAGS)
@@ -868,6 +870,7 @@ test_mortality_rates_CXXFLAGS = $(AM_CXXFLAGS)
test_name_value_pairs_SOURCES = \
$(common_test_objects) \
facets.cpp \
+ miscellany.cpp \
name_value_pairs.cpp \
name_value_pairs_test.cpp
test_name_value_pairs_CXXFLAGS = $(AM_CXXFLAGS)
@@ -881,6 +884,7 @@ test_ncnnnpnn_CXXFLAGS = $(AM_CXXFLAGS)
test_numeric_io_SOURCES = \
$(common_test_objects) \
+ miscellany.cpp \
numeric_io_test.cpp \
timer.cpp
test_numeric_io_CXXFLAGS = $(AM_CXXFLAGS)
@@ -929,6 +933,7 @@ test_premium_tax_LDADD = \
test_print_matrix_SOURCES = \
$(common_test_objects) \
facets.cpp \
+ miscellany.cpp \
print_matrix_test.cpp
test_print_matrix_CXXFLAGS = $(AM_CXXFLAGS)
@@ -1054,6 +1059,7 @@ test_tn_range_SOURCES = \
$(common_test_objects) \
datum_base.cpp \
facets.cpp \
+ miscellany.cpp \
tn_range_test.cpp \
tn_range_test_aux.cpp
test_tn_range_CXXFLAGS = $(AM_CXXFLAGS)
@@ -1061,6 +1067,7 @@ test_tn_range_CXXFLAGS = $(AM_CXXFLAGS)
test_value_cast_SOURCES = \
$(common_test_objects) \
facets.cpp \
+ miscellany.cpp \
value_cast_test.cpp
test_value_cast_CXXFLAGS = $(AM_CXXFLAGS)
@@ -1078,6 +1085,7 @@ test_wx_new_CXXFLAGS = $(AM_CXXFLAGS)
test_xml_serialize_SOURCES = \
$(common_test_objects) \
facets.cpp \
+ miscellany.cpp \
timer.cpp \
xml_lmi.cpp \
xml_serialize_test.cpp
diff --git a/numeric_io_traits.hpp b/numeric_io_traits.hpp
index 93ea6a3..ebfd102 100644
--- a/numeric_io_traits.hpp
+++ b/numeric_io_traits.hpp
@@ -26,6 +26,7 @@
#include "bourn_cast.hpp"
#include "ieee754.hpp" // is_infinite<>()
+#include "miscellany.hpp" // rtrim()
#include <algorithm> // max()
#include <cmath> // fabs(), log10()
@@ -92,15 +93,10 @@ inline int floating_point_decimals(T t)
inline std::string simplify_floating_point(std::string const& s)
{
- std::string::const_reverse_iterator ri = s.rbegin();
- loop:
- switch(*ri)
- {
- case '0': ++ri; goto loop;
- case '.': ++ri;
- default : ;
- }
- return std::string(s.begin(), ri.base());
+ std::string z(s);
+ rtrim(z, "0");
+ rtrim(z, ".");
+ return z;
}
/// Traits for conversion between arithmetic types and strings.
diff --git a/objects.make b/objects.make
index 6960d89..04e4f9b 100644
--- a/objects.make
+++ b/objects.make
@@ -512,6 +512,7 @@ any_member_test$(EXEEXT): \
$(common_test_objects) \
any_member_test.o \
facets.o \
+ miscellany.o \
assert_lmi_test$(EXEEXT): \
$(common_test_objects) \
@@ -642,6 +643,7 @@ gpt_test$(EXEEXT): \
gpt_commutation_functions.o \
gpt_test.o \
ihs_irc7702.o \
+ miscellany.o \
timer.o \
handle_exceptions_test$(EXEEXT): \
@@ -772,6 +774,7 @@ name_value_pairs_test$(EXEEXT): \
$(boost_filesystem_objects) \
$(common_test_objects) \
facets.o \
+ miscellany.o \
name_value_pairs.o \
name_value_pairs_test.o \
@@ -781,6 +784,7 @@ ncnnnpnn_test$(EXEEXT): \
numeric_io_test$(EXEEXT): \
$(common_test_objects) \
+ miscellany.o \
numeric_io_test.o \
timer.o \
@@ -824,6 +828,7 @@ premium_tax_test$(EXEEXT): \
print_matrix_test$(EXEEXT): \
$(common_test_objects) \
facets.o \
+ miscellany.o \
print_matrix_test.o \
product_file_test$(EXEEXT): \
@@ -935,12 +940,14 @@ tn_range_test$(EXEEXT): \
$(common_test_objects) \
datum_base.o \
facets.o \
+ miscellany.o \
tn_range_test.o \
tn_range_test_aux.o \
value_cast_test$(EXEEXT): \
$(common_test_objects) \
facets.o \
+ miscellany.o \
value_cast_test.o \
vector_test$(EXEEXT): \
@@ -956,6 +963,7 @@ xml_serialize_test$(EXEEXT): \
$(common_test_objects) \
$(xmlwrapp_objects) \
facets.o \
+ miscellany.o \
timer.o \
xml_lmi.o \
xml_serialize_test.o \