[Performance] Why D's std.string.atoi is 4x slower than std.c.stdlib.atoi?

Andrey Khropov andrey.khropov at gmail.com
Sat Nov 11 12:24:29 PST 2006


Consider the following code:
-------------------------------------------------
import std.stdio, std.string, std.c.stdlib, std.perf;

int DStdLib(char[] cs)
{
    int res = 0;    
    for(int i = 0; i < 10000000; i++)
      res += std.string.atoi(cs);
         
    return res;
}

int CStdLib(char* cs)
{
    int res = 0;    
   
    for(int i = 0; i < 10000000; i++)
      res += std.c.stdlib.atoi(cs);    
      
    return res;
}

void main()
{
  auto t = new HighPerformanceCounter();
  
  int nIter = 5;
  
  int meanTime = 0;
  
  for(int it = 0; it <= nIter; ++it) {
    t.start();
    
    int res = DStdLib("123");
    
    t.stop();
    
    writefln("D-StdLib: res = ", res, ", ", t.milliseconds() ," ms elapsed ");
    
    if( it )
      meanTime += t.milliseconds();
  }
    
  writefln("D-StdLib:" , meanTime/nIter ," ms elapsed (mean).");
  
  meanTime = 0;
  
  for(int it = 0; it <= nIter; ++it) {
    t.start();
    
    int res = CStdLib("123");
    
    t.stop();
    
    writefln("C-StdLib: res = ", res, ", ", t.milliseconds() ," ms elapsed ");
    
    if( it )
      meanTime += t.milliseconds();
  }
    
  writefln("C-StdLib:" , meanTime/nIter ," ms elapsed (mean).");
}

-------------------------------------------------
On my machine (P-M 1.7 Dothan) the mean times are:

D-StdLib:1695 ms elapsed (mean).
C-StdLib:374 ms elapsed (mean).

Why is it so? What could be done?

-- 
AKhropov



More information about the Digitalmars-d mailing list