30-120 seconds sounds surprisingly long for ~368 attempts, do you know which part(s) the slowness comes from?
Should be under 5 seconds in C or C++ using gmp
From looking at the code, the overhead will be from repeatedly invoking git as a subprocess.