My AoC program in D is just as fast as in Python

MrJay themrjcraft at gmail.com
Tue Nov 25 22:19:19 UTC 2025


On Monday, 24 November 2025 at 14:37:22 UTC, Jabba Laci wrote:
> I implemented [Advent of Code 2015, Day 6, Part 
> 1](https://adventofcode.com/2015/day/6) in D. The code is here: 
> https://github.com/jabbalaci/AdventOfCode2015/tree/main/day06/d/part1 . It runs for 3 seconds. I found it a bit much, so I also wrote it in Python. To my surprise, the Python program also ran for 3 seconds. I expected that the D program should be much faster.
>
> I used a dictionary, where the key is a `Tuple!(int, int)`. 
> Maybe its hashing is too slow? How could it be sped up?
>
> Another question: for `process_line()`, I pass the dictioary by 
> reference, since I want to modify the dictionary inside this 
> procedure. Without `ref`, the original `d` in `main()` remained 
> empty. However, I've already written programs when I passed a 
> dictionary without `ref` and the changes to the dictionary were 
> visible outside of the procedure. What's the rule here?
>

after some investigating it just seems that its mostly down to 
using a hashtable
in the process line function
2540 msecs go by
2394 msecs are entirely from the if statemante with the AAs

when I profiled I also saw a lot of AA functions being called

you effectively are not comparing D vs Python
you are comparing pythons hashtable implementation vs Ds 
hashtable implementation

Python functions and datatypes can be highly optimized C code I 
believe the hashtable is one such case,
and D is still the same speed and faster depending on computer
using your code D was roughly twice as fast as Python on my 
computer

when you are comparing two languages you have to be careful to 
not accidentally add accidental bottlenecks, where you are not 
comparing the bottle neck rather than the languages



More information about the Digitalmars-d-learn mailing list