Which is better, PyPy or Python?

When it becomes TLE with Python, you can get AC with PyPy, and conversely, when you become TLE with PyPy, you can get AC with Python.

Example of AC in Python and TLE in PyPy

In Problem A of AtCoder Typical Contest 001, there is a problem of depth-first search.

So I wrote the following code.

test.py


import sys

sys.setrecursionlimit(500*500)
def dfs(px,py):
	if px < 0 or py < 0 or px > w-1 or py > h-1:
		return False

	if c[py][px] == '#':
		return False
	if c[py][px] == 'g':
		return True

	c[py][px] = '#'

	if dfs(px-1,py):
		return True
	if dfs(px,py-1):
		return True
	if dfs(px+1,py):
		return True
	if dfs(px,py+1):
		return True

	return False

h,w = map(int,input().split())

c = []
for i in range(h):
	s = input()
	x = s.find('s')
	if x != -1:
		start = [x,i]
	c.append(list(s))

sx = int(start[0])
sy = int(start[1])

if dfs(sx,sy):
	print('Yes')
else:
	print('No')

I think that it is a fairly straightforward way of writing as a depth-first search.

As a result of submitting this, it became as follows.

スクリーンショット 2020-03-06 20.45.37.png

I got AC in Python3, but it has become TLE in PyPy3.

It seems that ** PyPy is incompatible with recursive functions **, which is the cause.

You can take AC either way by using the stack quietly without using recursive functions.

Example of AC with PyPy and TLE with Python

AtCoder Beginner Contest 153 E Problem was a problem that could be solved using dynamic programming.

So I wrote the following code.

test.py


H,N= map(int,input().split())

a = []
b = []

for i in range(N):
  a_dash,b_dash = map(int,input().split())
  a.append(a_dash)
  b.append(b_dash)

#dp[i+1][j] =Become a monster with physical strength j with magic up to the i-th
#Minimum amount of magical power that can be won
INF = 10**9
dp = [[INF for _ in range(H+1)] for _ in range(N+1)]

#Monsters with 0 health have died from the beginning
for i in range(N+1):
  dp[i][0] = 0

for i in range(N):
  for j in range(H+1):
    if a[i] > j:
      dp[i+1][j] = min(dp[i][j],b[i])
    else:
      dp[i+1][j] = min(dp[i][j],dp[i+1][j-a[i]]+ b[i])

print(dp[N][H])

Without any special ingenuity, the code is simply similar to the unlimited number knapsack problem.

When I submitted this, the following results were returned.

スクリーンショット 2020-03-06 20.40.32.png

This time it became AC with PyPy and TLE with Python3.

Of course, there is also a way to solve it in Python.

Which one should I use after all?

** I think the correct answer is to write a beautiful answer that allows you to get AC regardless of which one you use.

However, I'm a beginner in competition pros, so I'll use PyPy for the time being, and use Python when using recursive functions **.

By the way, PyPy seems to have the disadvantage that some libraries written in C language cannot be used.

Finally

Since I am a beginner in competition professionals, please point out any deficiencies in the above contents.

Recommended Posts

Which is better, PyPy or Python?
Which is better, python standard input receiving input () or sys.stdin?
[Beginners are worried] Which is better, Ruby, PHP or Python?
Which is faster, Python shuffle or sample?
[Linux] End of process or job, which is better?
Golang vs. Python – Is Golang Better Than Python?
Analysis by Bayesian inference (1) ... Which is better, A or B?
[Python] Which is executed first, the class variable or __init__?
Determining which OS is running Python
Python is easy
What is python
Python is instance
What is Python
[Python] Which should be used, return or return None
python int is infinite
Python> list> extend () or + =
Python from or import
python> check NoneType or not> if a == None:> if a is None:
Memorandum @ Python OR Seminar
[Python] What is virtualenv
Raspberry Pi with Elixir, which is cooler than Python
When converting a string (a-> b) in Python, which is faster, if statement or dictionary type?
Which is the most popular python visualization tool after all?
Which should I study, R or Python, for data analysis?
[Python] Is the zero but non-empty object Truthy or Fallsy?
Python round is not strictly round
[Python] Debugging is more efficient!
What is Minisum or Minimax?
Python 3.4 or later standard pip
How Python __dict__ is used
pypy bool type is slow
Python is painful. But use
Python is an adult language
Memorandum @ Python OR Seminar: matplotlib
Python list is not a list
[Python] Python and security-① What is Python?
Memorandum @ Python OR Seminar: Pulp
Python release cycle is faster!
Python bitwise operator and OR
[Python] * args ** What is kwrgs?
Memorandum @ Python OR Seminar: Pandas
Memorandum @ Python OR Seminar: scikit-learn
Ruby's `` like Python. 2.6 or later
Identity and equivalence Python is and ==
Python or and and operator trap
What is a python map?
Python Basic Course (1 What is Python)
If a beginner learns R or Python this December 2019, which one?
Which is better, PyPy or Python?
Which is faster, Python shuffle or sample?
Grammar summary that is often forgotten with matplotlib
[Linux] End of process or job, which is better?
Which is better, python standard input receiving input () or sys.stdin?