Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
divmod_threshold.py57 linesDownload Raw Back to scripts
1#!/usr/bin/env python32#3# Determine threshold for switching from longobject.c divmod to4# _pylong.int_divmod().5 6from random import randrange7from time import perf_counter as now8from _pylong import int_divmod as divmod_fast9 10BITS_PER_DIGIT = 3011 12 13def rand_digits(n):14    top = 1 << (n * BITS_PER_DIGIT)15    return randrange(top >> 1, top)16 17 18def probe_den(nd):19    den = rand_digits(nd)20    count = 021    for nn in range(nd, nd + 3000):22        num = rand_digits(nn)23        t0 = now()24        e1, e2 = divmod(num, den)25        t1 = now()26        f1, f2 = divmod_fast(num, den)27        t2 = now()28        s1 = t1 - t029        s2 = t2 - t130        assert e1 == f131        assert e2 == f232        if s2 < s1:33            count += 134            if count >= 3:35                print(36                    "for",37                    nd,38                    "denom digits,",39                    nn - nd,40                    "extra num digits is enough",41                )42                break43        else:44            count = 045    else:46        print("for", nd, "denom digits, no num seems big enough")47 48 49def main():50    for nd in range(30):51        nd = (nd + 1) * 10052        probe_den(nd)53 54 55if __name__ == '__main__':56    main()57 
codekingpro/portable-devtools · Team Ai