Why is D significantly slower than C# in this instance?

Artjom sda20036 at gmail.com
Mon Apr 10 21:46:25 UTC 2023


On Monday, 10 April 2023 at 21:40:40 UTC, H. S. Teoh wrote:
> On Mon, Apr 10, 2023 at 09:18:59PM +0000, Artjom via 
> Digitalmars-d wrote:
>> I have written this simple bruteforce algorithm that finds max 
>> sum of subsequence in some sequence, both in C# and D. And 
>> when the size of array is 1000000 elements - D is 20 seconds 
>> lated. Why?
>
> Which compiler are you using and what are the compiler options?
>
>
>> D code (algorithm):
>> '
>>     public int solveBruteForce()
>>     {
>>         int currMax = arr[0], currSum;
>>         foreach (_; arr)
>>         {
>>             currSum = 0;
>>             foreach (int el; arr)
>>             {
>>                 currSum += el;
>>                 currMax = max(currSum, currMax);
>>             }
>>         }
>> 
>>         return currMax;
>>     }
>> '
>> D code (measuring exec time):
>> '
>>     double measureTime()
>>     {
>>         StopWatch sw;
>>         sw.start();
>>         solveDynamic();
>
> This doesn't call the above function solveBruteForce. What's 
> the definition of solveDynamic?
>
>
>>         double sec = (to!double(sw.peek.total!"msecs") / 1000) 
>> +
>> (to!double(sw.peek.total!"usecs") / 1000000);
>>         return sec;
>>     }
> [...]
>
>
> T

Sorry, accidentaly pasted solveDynamic() insted of 
solveBruteForce(), it's solveBruteForce() in the code though.
Im using DMD and compile like "dmd .\source\app.d 
.\classes\solver.d"


More information about the Digitalmars-d mailing list