pypy 的下载、安装、测试
一、Windows 中 pypy 下载与安装
下载链接:https://www.pypy.org/download.html
二、pip 的安装
1、例如把下载下来的 zip 直接解压到 D 盘 D:\pypy3.6-v7.3.0-win32
2、pypy 的 pip 安装命令:pypy3 -m ensurepip
三、创建 pypy 虚拟环境并简单使用
mkvirtualenv -p d:\pypy3.6-v7.3.0-win32\pypy3.exe pypy3_env
# 进入虚拟环境之后 pip 的使用命令就是
pypy -m pip [command]
四、排序算法测试
import random
import time
from mylog import logger
def clock(fmt='{name:>15} [{time_delta:0.9f}s] -----> {ret}'):
def run_time(func):
def wrapper(*args, **kwargs):
start = time.time()
ret = func(*args, **kwargs)
end = time.time()
time_delta = end - start
name = func.__name__
logger.info(fmt.format(**locals()))
return ret
return wrapper
return run_time
@clock()
def get_nums(n):
result = []
while n >= 0:
result.append(random.randint(1, 100000))
n -= 1
return result
@clock()
def bubble(nums): # 1.789999962s
n = len(nums)
for i in range(n - 1):
for j in range(n - i - 1):
if nums[j] > nums[j + 1]:
nums[j], nums[j + 1] = nums[j + 1], nums[j]
logger.debug(nums)
@clock()
def insert_sort(nums): # 0.991999865s
n = len(nums)
for i in range(1, n):
if nums[i] < nums[i - 1]:
current = nums[i]
for j in range(i - 1, -1, -1):
if current < nums[j]:
nums[j + 1] = nums[j]
nums[j] = current
logger.debug(nums)
@clock()
def shell_sort(nums):
step = len(nums) // 2
while step > 0:
for i in range(step, len(nums)):
while i >= step and nums[i] < nums[i - step]:
nums[i], nums[i - step] = nums[i - step], nums[i]
i -= step
step //= 2
logger.debug(nums)
@clock()
def choose_insert(nums): # 0.683000088
n = len(nums)
for i in range(0, n):
current_pos = i
for j in range(i + 1, n):
if nums[current_pos] > nums[j]:
current_pos = j
nums[current_pos], nums[i] = nums[i], nums[current_pos]
logger.debug(nums)
if __name__ == '__main__':
# 随机生成10000个数,看插入排序,冒泡排序,选择排序,希尔排序的时间
datas = get_nums(10000)
insert_sort(datas[:])
bubble(datas[:])
choose_insert(datas[:])
shell_sort(datas[:])
python 自带的 CPython 解释器运行时间
pypy 解释器运行时间
插入排序 | 冒泡排序 | 选择排序 | 希尔排序 | |
---|---|---|---|---|
CPython | 4.044999838s | 7.256999969s | 2.706000090s | 0.052000046s |
Pypy | 0.245000124s | 0.221999884s | 0.100999832s | 0.015999794s |