Why is D significantly slower than C# in this instance?
Steven Schveighoffer
schveiguy at gmail.com
Tue Apr 11 01:49:59 UTC 2023
On 4/10/23 5:29 PM, Artjom wrote:
>> 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;
> }
> ```
...
>> C# code (alg):
> ```
> int EachOther(int[] array)
> {
> int current_max = array[0];
> for (int i = 0; i < array.Length; i++)
> {
> int subset_sum = 0;
> for (int j = i; j < array.Length; j++)
> {
> subset_sum += array[j];
> current_max = Math.Max(subset_sum, current_max);
> }
> }
> return current_max;
> }
> ```
>
You realize these are 2 different loop setups?
Lining up pseudocode so you can see:
D: foreach(i; 0 .. len) foreach(j; 0 .. len)
C#: foreach(i; 0 .. len) foreach(j; i .. len)
-Steve
More information about the Digitalmars-d
mailing list