lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 2202d220 09/11: Reimplement ledger_format() i


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 2202d220 09/11: Reimplement ledger_format() in terms of duff_fmt()
Date: Fri, 29 Apr 2022 11:59:34 -0400 (EDT)

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

    Reimplement ledger_format() in terms of duff_fmt()
    
    Speed:
    
    i686-w64-mingw32
      construct        : 2.373e-03 s mean;       2362 us least of 100 runs
      make_evaluator() : 8.506e-03 s mean;       8027 us least of 100 runs
      write_tsv()      : 4.501e-03 s mean;       4317 us least of 100 runs
      mete_format()    : 1.101e-02 s mean;      10770 us least of  91 runs
    
    x86_64-pc-linux-gnu
      construct        : 5.123e-04 s mean;        499 us least of 100 runs
      make_evaluator() : 5.317e-03 s mean;       4575 us least of 100 runs
      write_tsv()      : 1.401e-03 s mean;       1386 us least of 100 runs
      mete_format()    : 6.627e-03 s mean;       6571 us least of 100 runs
    
    Improvement, compared to last commit: (old-new)/old
    
                       i686-w64-mingw32   x86_64-pc-linux-gnu
                         mean   least        mean   least
      make_evaluator()   26.9%   22.5%       27.0%   34.7%
      mete_format()      16.9%   16.2%       21.1%   21.4%
    
    What really matters is the improvement in make_evaluator(), which is
    implemented in terms of the reimplemented ledger_format(); mete_format()
    measures the speed of ledger_format(), but it is not necessarily a
    representative use case.
    
    This venerable article:
      http://www.gotw.ca/publications/mill19.htm
      The String Formatters of Manor Farm
    measured snprintf() as about ten times faster than std::stringstream.
    It may reasonably be concluded that the standard library has gotten
    much faster since November 2001, its overhead having shrunk from 1000%
    to 25%.
---
 ledger_text_formats.cpp | 23 ++---------------------
 1 file changed, 2 insertions(+), 21 deletions(-)

diff --git a/ledger_text_formats.cpp b/ledger_text_formats.cpp
index e761e84d..0dd35680 100644
--- a/ledger_text_formats.cpp
+++ b/ledger_text_formats.cpp
@@ -31,6 +31,7 @@
 #include "comma_punct.hpp"
 #include "configurable_settings.hpp"    // 
effective_calculation_summary_columns()
 #include "contains.hpp"
+#include "duff_fmt.hpp"
 #include "global_settings.hpp"
 #include "ledger.hpp"
 #include "ledger_invariant.hpp"
@@ -1177,33 +1178,13 @@ std::string ledger_format
     ,std::pair<int,oenum_format_style> f
     )
 {
-    static std::stringstream interpreter = []
-        {
-        std::stringstream ss {};
-        std::locale loc;
-        std::locale new_loc(loc, ::new comma_punct);
-        ss.imbue(new_loc);
-        ss.setf(std::ios_base::fixed, std::ios_base::floatfield);
-        return ss;
-        } ();
-    interpreter.str(std::string{});
-    interpreter.clear();
-
-    interpreter.precision(f.first);
-    std::string s;
     switch(f.second)
         {
         case oe_format_normal:     {} break; // Deliberately do nothing.
         case oe_cents_as_dollars:  {d /= 100;} break;
         case oe_format_percentage: {d *= 100;} break;
         }
-
-    interpreter << d;
-    interpreter >> s;
-    if(!interpreter.eof())
-        {
-        alarum() << "Formatting error." << LMI_FLUSH;
-        }
+    std::string s = duff_fmt(d, f.first);
 
     if(oe_format_percentage == f.second)
         {



reply via email to

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