Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

runing following code in pypy 7.3.12 is 3 times slower than in python 3.8 #20

Open
l1t1 opened this issue Dec 29, 2023 · 0 comments
Open

Comments

@l1t1
Copy link

l1t1 commented Dec 29, 2023

# -*- coding:gbk -*- #
import itertools
cards=[[i,j,k,l] for i in range(1,11)  for j in range(i,11)for k in range(j,11)for l in range(k,11)]
def twentyfour(cards):
    '''史上最短计算24点代码'''
    for nums in itertools.permutations(cards): # 四个数
        for ops in itertools.product('+-*/', repeat=3): # 三个运算符(可重复!)
            # 构造三种中缀表达式 (bsd)
            bds1 = '({0}{4}{1}){5}({2}{6}{3})'.format(*nums, *ops)  # (a+b)*(c-d)
            bds2 = '(({0}{4}{1}){5}{2}){6}{3}'.format(*nums, *ops)  # (a+b)*c-d
            bds3 = '{0}{4}({1}{5}({2}{6}{3}))'.format(*nums, *ops)  #  a/(b-(c/d))
            for bds in [bds1, bds2, bds3]: # 遍历
                try:
                    if abs(eval(bds) - 24.0) < 1e-10:   # eval函数
                        return bds
                except ZeroDivisionError: # 零除错误!
                    continue
    return 'Not found!'


for card in cards:
    print(card,twentyfour(card))

I run in the way:

D:\>python
Python 3.8.8 (tags/v3.8.8:024d805, Feb 19 2021, 13:18:16) [MSC v.1928 64 bit (AMD64)] on win32
timer64 python 24-short.txt

Kernel  Time =     0.062 =    0%
User    Time =    13.447 =   99%
Process Time =    13.509 =  100%    Virtual  Memory =      8 MB
Global  Time =    13.503 =  100%    Physical Memory =     12 MB

D:\>pypy3.10-v7.3.12-win64\pypy
Python 3.10.12 (af44d0b8114cb82c40a07bb9ee9c1ca8a1b3688c, Jun 15 2023, 15:42:22)
[PyPy 7.3.12 with MSC v.1929 64 bit (AMD64)] on win32

timer64 pypy3.10-v7.3.12-win64\pypy 24-short.txt
Kernel  Time =     0.046 =    0%
User    Time =    38.516 =   99%
Process Time =    38.563 =   99%    Virtual  Memory =     36 MB
Global  Time =    38.655 =  100%    Physical Memory =     34 MB
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant