[Python] Fast Prime List Functions →→→→→进入此内容的聊天室

来自 , 2020-05-28, 写在 Python, 查看 180 次.
URL http://www.code666.cn/view/d8114063
  1. '''primelist_timing1.py
  2. timing some very fast primelist functions
  3. tested with Python27 and Python32  by  vegaseat
  4. '''
  5. import timeit
  6. import numpy
  7. import sys
  8. print("Python version:\n %s\n" % sys.version)
  9. def time_function(stmt, setup):
  10.     """
  11.    use module timeit to time functions
  12.    """
  13.     # to enable garbage collection start setup with 'gc.enable();'
  14.     #setup = 'gc.enable();' + setup
  15.     t = timeit.Timer(stmt, setup)
  16.     times = 10000
  17.     num = 100
  18.     # times*num has to be 1000000 to get time in microseconds/pass
  19.     # (lower num is a little less precise but saves time)
  20.     elapsed = (times * t.timeit(number=num))
  21.     print("%-20s --> %0.2f microseconds/pass" % (stmt, elapsed))
  22. def primelist_bw(n):
  23.     """
  24.    returns  a list of primes < n
  25.    by Bill Woods
  26.    """
  27.     sieve = [True] * (n>>1)
  28.     for x in range(3, int(n**0.5)+1, 2):
  29.         if sieve[x>>1]:
  30.             sieve[x*x//2::x] = [False] * ((n-x*x-1)//(x<<1)+1)
  31.     return [2] + [(x<<1)+1 for x in range(1, n>>1) if sieve[x]]
  32. def primelist_ds(n):
  33.     """
  34.    returns  a list of primes < n
  35.    """
  36.     sieve = [True] * (n>>1)
  37.     for x in range(3, int(n**0.5)+1, 2):
  38.         if sieve[x>>1]:
  39.             sieve[(x*x)>>1::x] = [False] * ((n-x*x-1)//(x<<1)+1)
  40.     return [2] + [(x<<1)+1 for x in range(1, n>>1) if sieve[x]]
  41. def primes_numpy(n):
  42.     """
  43.    requires module numpy and n>=6,
  44.    returns a list of primes 2 <= p < n
  45.    """
  46.     sieve = numpy.ones(n//3 + (n%6==2), dtype=numpy.bool)
  47.     sieve[0] = False
  48.     for x in range(int(n**0.5)//3+1):
  49.         if sieve[x]:
  50.             k = 3*x + 1|1
  51.             sieve[((k*k)//3)::2*k] = False
  52.             sieve[(k*k+4*k-2*k*(x&1))//3::2*k] = False
  53.     return numpy.r_[2,3,((3*numpy.nonzero(sieve)[0]+1)|1)].tolist()
  54. # time a function
  55. stmt = 'primelist_bw(1000000)'
  56. setup = 'from __main__ import primelist_bw'
  57. time_function(stmt, setup)
  58. # time a function
  59. stmt = 'primelist_ds(1000000)'
  60. setup = 'from __main__ import primelist_ds'
  61. time_function(stmt, setup)
  62. # time a function
  63. stmt = 'primes_numpy(1000000)'
  64. setup = 'from __main__ import primes_numpy, numpy'
  65. time_function(stmt, setup)
  66. print('-'*60)
  67. # additional test (show the last 5 primes) ...
  68. print(primelist_bw(1000000)[-5:])
  69. print(primelist_ds(1000000)[-5:])
  70. print(primes_numpy(1000000)[-5:])
  71. '''result ...
  72. Python version:
  73. 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]
  74. primelist_bw(1000000) --> 75969.22 microseconds/pass
  75. primelist_ds(1000000) --> 73669.90 microseconds/pass
  76. primes_numpy(1000000) --> 9016.74 microseconds/pass
  77. ------------------------------------------------------------
  78. [999953, 999959, 999961, 999979, 999983]
  79. [999953, 999959, 999961, 999979, 999983]
  80. [999953, 999959, 999961, 999979, 999983]
  81. Python version:
  82. 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)]
  83. primelist_bw(1000000) --> 77284.28 microseconds/pass
  84. primelist_ds(1000000) --> 75547.38 microseconds/pass
  85. primes_numpy(1000000) --> 28055.11 microseconds/pass
  86. ------------------------------------------------------------
  87. [999953, 999959, 999961, 999979, 999983]
  88. [999953, 999959, 999961, 999979, 999983]
  89. [999953, 999959, 999961, 999979, 999983]
  90. '''
  91. #//python/5181

回复 "Fast Prime List Functions"

这儿你可以回复上面这条便签

captcha