FSharp runs my algorithm slower than Python

后端 未结 4 1660
广开言路
广开言路 2020-12-04 08:44

Years ago, I solved a problem via dynamic programming:

https://www.thanassis.space/fillupDVD.html

The solution was coded in Python.

As part of expand

4条回答
  •  臣服心动
    2020-12-04 09:31

    Hmm.. if the hashtable is the major bottleneck, then it is properly the hash function itself. Havn't look at the specific hash function but For one of the most common hash functions namely

    ((a * x + b) % p) % q

    The modulus operation % is painfully slow, if p and q is of the form 2^k - 1, we can do modulus with an and, add and a shift operation.

    Dietzfelbingers universal hash function h_a : [2^w] -> [2^l]

    lowerbound(((a * x) % 2^w)/2^(w-l))

    Where is a random odd seed of w-bit.

    It can be computed by (a*x) >> (w-l), which is magnitudes of speed faster than the first hash function. I had to implement a hash table with linked list as collision handling. It took 10 minutes to implement and test, we had to test it with both functions, and analyse the differens of speed. The second hash function had as I remember around 4-10 times of speed gain dependend on the size of the table. But the thing to learn here is if your programs bottleneck is hashtable lookup the hash function has to be fast too

提交回复
热议问题