Slower than Python

cvk012c cvk012c at motorolasolutions.com
Fri Mar 1 16:19:35 PST 2013


On Friday, 1 March 2013 at 21:52:13 UTC, bearophile wrote:
> cvk012c:
>
>> I think that similar Perl and Java scripts will outperform D 
>> easily.
>> Thanks Andrei and simendsjo for a quick response though.
>
> Why don't you write a Java version? It takes only few minutes, 
> and you will have one more data point.
>

You are right. Why not. But instead of using Java split() method 
I used combination of indexOf() and substring() methods to do the 
same job. The reason: Java split method implemented as a regular 
expression which will be unfair to compare to D splitter. Again, 
I created a similar D version of the script, compiled it with all 
suggested options:  -release -O -inline -noboundscheck and this 
time D version is more then twice slower than Java: 8.4 seconds 
vs 4.
D experts, please, take a look at my code and tell me what is 
wrong with it.

Java version:

public class Test
{
   public static void main(String[] args)
   {
     String message = "REGISTER sip:comm.example.com 
SIP/2.0\r\nContent-Length: 0\r\nContact: 
<sip:12345 at 10.1.3.114:59788;transport=tls>;expires=4294967295;events=\"message-summary\";q=0.9\r\nTo: 
<sip:12345 at comm.example.com>\r\nUser-Agent: (\"VENDOR=MyCompany\" 
\"My User Agent\")\r\nMax-Forwards: 70\r\nCSeq: 1 
REGISTER\r\nVia: SIP/2.0/TLS 
10.1.3.114:59788;branch=z9hG4bK2910497772630690\r\nCall-ID: 
2910497622026445\r\nFrom: 
<sip:12345 at comm.example.com>;tag=2910497618150713\r\n\r\n";
     long t1 = System.currentTimeMillis();
     for (int i=0; i<10000000; i++)
     {
       int index1 = 0;
       while (true)
       {
         int index2 = message.indexOf("\r\n", index1);
         if (index2 == -1)
           break;
         String notused = message.substring(index1, index2);
         index1 = index2+2;
       }
     }
     System.out.println(System.currentTimeMillis()-t1);
   }
}



D version:

import std.stdio,std.string,std.datetime;

void main()
{
   auto message = "REGISTER sip:example.com 
SIP/2.0\r\nContent-Length: 0\r\nContact: 
<sip:12345 at 10.1.3.114:59788;transport=tls>;expires=4294967295;events=\"message-summary\";q=0.9\r\nTo: 
<sip:12345 at comm.example.com>\r\nUser-Agent: (\"VENDOR=MyCompany\" 
\"My User Agent\")\r\nMax-Forwards: 70\r\nCSeq: 1 
REGISTER\r\nVia: SIP/2.0/TLS 
10.1.3.114:59788;branch=z9hG4bK2910497772630690\r\nCall-ID: 
2910497622026445\r\nFrom: 
<sip:12345 at comm.example.com>;tag=2910497618150713\r\n\r\n";
   auto t1 = Clock.currTime();
   for (int i=0; i<10000000; i++)
   {
     auto substring = message;
     while (true)
     {
       auto index = indexOf(substring, "\r\n");
       if (index == -1)
         break;
       auto notused = substring[0..index];
       substring = substring[index+2..$];
     }
   }
   writeln(Clock.currTime()-t1);
}


More information about the Digitalmars-d mailing list