Jump to page: 1 2 3
Thread overview
April 01

I am a python programmer and I am enjoying Dlang and learning some programming insights on the way, thank everyone.

I have no formal education and also program JS and PHP.

Watching a video where a guy programs some simple code in Python and the same code in Go and compares speed I thought that could be some nice exercise for my learning path and successfully ported code to Dlang (hope so)

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.

1- I am doing something wrong in my code?
2- Do I have wrong expectations about Dlang?

Thanks in advance.

This is the video: https://www.youtube.com/watch?v=1Sban1F45jQ
This is my D code:

import std.stdio;
import std.random;
import std.datetime.stopwatch : benchmark, StopWatch, AutoStart;
import std.algorithm;

void main()
{
    auto sw = StopWatch(AutoStart.no);
    sw.start();
    int[] mylist;
    for (int number = 0; number < 100000; ++number)
    {
        auto rnd = Random(unpredictableSeed);
        auto n = uniform(0, 100, rnd);
        mylist ~= n;
    }
    mylist.sort();
    sw.stop();
    long msecs = sw.peek.total!"msecs";
    writefln("%s", msecs);
}
import time
import random

start = time.time()
mylist = []
for _ in range(100000):
    mylist.append(random.randint(0,100))
mylist.sort()
end = time.time()
print(f"{(end-start)*1000}ms")
April 01

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).

April 01

On Thursday, 1 April 2021 at 16:52:17 UTC, Nestor wrote:

>

I am a python programmer and I am enjoying Dlang and learning some programming insights on the way, thank everyone.

[...]

Could you also post the python code for comparison?

April 01

On Thursday, 1 April 2021 at 17:16:06 UTC, Imperatorn wrote:

>

On Thursday, 1 April 2021 at 16:52:17 UTC, Nestor wrote:

>

I am a python programmer and I am enjoying Dlang and learning some programming insights on the way, thank everyone.

[...]

Could you also post the python code for comparison?

Omg I totally missed it lol 😂

April 01
On 4/1/21 10:15 AM, ag0aep6g wrote:

> Move `auto rnd = ...;` out of the loop, and you will get better times.

Doing that reduces the time about 15 fold.

Using Appender reduces it further a tiny bit:

import std.array;
// ...
    Appender!(int[]) mylist;
// ...
    mylist.data.sort();

Ali

April 01

On Thursday, 1 April 2021 at 16:52:17 UTC, Nestor wrote:

>

I am a python programmer and I am enjoying Dlang and learning some programming insights on the way, thank everyone.

I have no formal education and also program JS and PHP.

Watching a video where a guy programs some simple code in Python and the same code in Go and compares speed I thought that could be some nice exercise for my learning path and successfully ported code to Dlang (hope so)

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.

1- I am doing something wrong in my code?
2- Do I have wrong expectations about Dlang?

Thanks in advance.

This is the video: https://www.youtube.com/watch?v=1Sban1F45jQ
This is my D code:

import std.stdio;
import std.random;
import std.datetime.stopwatch : benchmark, StopWatch, AutoStart;
import std.algorithm;

void main()
{
    auto sw = StopWatch(AutoStart.no);
    sw.start();
    int[] mylist;
    for (int number = 0; number < 100000; ++number)
    {
        auto rnd = Random(unpredictableSeed);
        auto n = uniform(0, 100, rnd);
        mylist ~= n;
    }
    mylist.sort();
    sw.stop();
    long msecs = sw.peek.total!"msecs";
    writefln("%s", msecs);
}
import time
import random

start = time.time()
mylist = []
for _ in range(100000):
    mylist.append(random.randint(0,100))
mylist.sort()
end = time.time()
print(f"{(end-start)*1000}ms")

Now when I actually read what you wrote I tested moving auto rnd = Random(unpredictableSeed) outside the loop and got 481 ms for the first version vs 34 ms for the other.


auto sw = StopWatch(AutoStart.no);
sw.start();
int[] mylist;

auto rnd = Random(unpredictableSeed);

for (int number = 0; number < 100000; ++number)
{
    auto n = uniform(0, 100, rnd);
    mylist ~= n;
}

mylist.sort();
sw.stop();

long msecs = sw.peek.total!"msecs";
writefln("%s", msecs);

Also, one thing you could do is to parallelize for even faster performance. I can show you how to do that later if you want to.

April 01

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.

Nice test. I'm new to D as well and can't comment on needed refactoring. To confirm your results I compiled the D example using:

gdc -O2 speed.d -o speed

and measured 129 ms for the D program and 63 ms for the python3 equivalent.

I'll be keen to see how this plays out since I'm using D as a faster alternative to python.

April 01

On Thursday, 1 April 2021 at 17:30:15 UTC, Chris Piker wrote:

>

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.

Nice test. I'm new to D as well and can't comment on needed refactoring. To confirm your results I compiled the D example using:

gdc -O2 speed.d -o speed

and measured 129 ms for the D program and 63 ms for the python3 equivalent.

I'll be keen to see how this plays out since I'm using D as a faster alternative to python.

If I make more changes in D I get below 10ms

April 01

On Thursday, 1 April 2021 at 17:30:15 UTC, Chris Piker wrote:

>

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.

Nice test. I'm new to D as well and can't comment on needed refactoring. To confirm your results I compiled the D example using:

gdc -O2 speed.d -o speed

and measured 129 ms for the D program and 63 ms for the python3 equivalent.

I'll be keen to see how this plays out since I'm using D as a faster alternative to python.

I have made 4 variants of the code and get:
20 ms (mylist ~= n and mylist.sort)
8 ms (mylist[number] = n and concurrent sort)
10 ms (parallel assignment and mylist.sort)
5 ms (parallel assignment and concurrent.sort)

Also make sure you build your application in release mode, this made quite some difference.

April 01

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.

Try using ldc2 instead of dmd:

ldc2 -O3 -release -boundscheck=off -flto=full -defaultlib=phobos2-ldc-lto,druntime-ldc-lto speed.d

should produce much better results.

« First   ‹ Prev
1 2 3