A stupid benchmark for a trivial idea

Paolo Carlini pcarlini@suse.de
Sat Nov 29 20:39:00 GMT 2003


Hi all,

so, before going out for a movie, I did this very stupid test:

////////////////////////

#include <cwchar>
#include <locale>

struct CtypeS: std::ctype<wchar_t>
{
  char
  do_narrow(wchar_t __wc, char __dfault) const
  {
    int __c = wctob(__wc);
    return (__c == EOF ? __dfault : static_cast<char>(__c));
  }
};


struct CtypeF: std::ctype<wchar_t>
{
  int cached[128];

  char
  do_narrow(wchar_t __wc, char __dfault) const
  {
    int __c;
    if (__wc < 128)
      __c = cached[__wc];
    else
      __c = wctob(__wc);

    return (__c == EOF ? __dfault : static_cast<char>(__c));
  }
};

int main()
{
  using namespace std;
 
  CtypeS cty; // CtypeF

  for (long i = 0; i < 100000000; ++i)
    cty.narrow(i % 128, '*');
}

//////////////////////

These are the numbers (-O2, P4-2400):

CtypeS
------
9.540u 0.000s 0:09.57 99.6%     0+0k 0+0io 191pf+0w

CtypeF
------
0.940u 0.000s 0:00.95 98.9%     0+0k 0+0io 191pf+0w


Yes, it's like it seems, 10x faster!

So... guys, let's converge to something *really* nice for the cute
caching mechanism inside narrow (widen) or otherwise... ;-)

Paolo.



More information about the Libstdc++ mailing list