Need for speed

ag0aep6g anonymous at example.com
Thu Apr 1 17:15:02 UTC 2021


On Thursday, 1 April 2021 at 16:52:17 UTC, Nestor wrote:
> I was hoping to beat my dear Python and get similar results to 
> Go, but that is not the case neither using rdmd nor running the 
> executable generated by dmd. I am getting values between 
> 350-380 ms, and 81ms in Python.
[...]
> ```
>     for (int number = 0; number < 100000; ++number)
>     {
>         auto rnd = Random(unpredictableSeed);
>         auto n = uniform(0, 100, rnd);
>         mylist ~= n;
>     }
> ```
>
> ```
> for _ in range(100000):
>     mylist.append(random.randint(0,100))
> ```

In the D version, you're re-seeding the random number generator 
on every loop. That takes time. You're not doing that in the 
Python version.

Move `auto rnd = ...;` out of the loop, and you will get better 
times. Or just use the default generator with `uniform(0, 100)`.


More information about the Digitalmars-d-learn mailing list