Slower than Python

anon123 z at z.z
Fri Mar 1 17:19:19 PST 2013


On Saturday, 2 March 2013 at 01:05:35 UTC, cvk012c wrote:
> On Saturday, 2 March 2013 at 00:47:02 UTC, Steven Schveighoffer 
> wrote:
>> On Fri, 01 Mar 2013 19:19:35 -0500, cvk012c 
>> <cvk012c at motorolasolutions.com> wrote:
>>
>>> 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.
>>
>> The issue is a combination of the fact that:
>> 1. splitter is designed for any range, not just strings.  Not 
>> an excuse really, but a string-specific version could be 
>> written that does better (clearly).
>
> In my latest version of D script I didn't use splitter at all. 
> I used string specific indexOf function. Still result is very 
> bad. For text based protocols, such as SIP, performance of 
> string manipulating functions is very important. Unfortunately, 
> looks like it is not D strongest point at this time.

My version is functionally equivalent, and measures 55 - 94 ms 
(depending on compiler; LDC is best). Your version performed in 
about 7s on my machine.

Similar optimization taking advantage of immutability can be done 
on your python translation that results in measurements of <1ms.


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<10_000_000; i++)
   {
     while (true)
     {
       auto index = indexOf(message, "\r\n");
       if (index == -1)
         break;
       auto notused = message[0..index];
       message = message[index+2..$];
     }
   }
   writeln(Clock.currTime()-t1);
}


More information about the Digitalmars-d mailing list