#🔒 PyPy Slow M1

68 messages · Page 1 of 1 (latest)

cyan nymph
glad frigateBOT
#

@cyan nymph

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sudden bough
#

In general, PyPy is not guaranteed to be faster at everything. It will be worse at some tasks

#

If you send the code as text I could try running it on my machine, which is not an M1

cyan nymph
cyan nymph
scenic finch
#

what's the result with timeit

sudden bough
#

Maybe pypy just has a slower hash table implementation

cyan nymph
sudden bough
# cyan nymph What are your test results? 👀
df@duckpond:/tmp$ pyenv local 3.12.3
df@duckpond:/tmp$ python test.py 
Eklenme süresi (20000000 eleman): 2.176011323928833 saniye
df@duckpond:/tmp$ pyenv local pypy3.10-7.3.15
df@duckpond:/tmp$ python test.py 
Eklenme süresi (20000000 eleman): 3.1895201206207275 saniye
df@duckpond:/tmp$ 
#

I removed one 0 because apparently I don't have enough RAM

#

!e

import time

def test_hash_table(size):
    start_time = time.time()

    # Hash tablosunu oluştur
    table = {}
    
    # Elemanları ekle
    for i in range(size):
        table[i] = i + 3
    
    end_time = time.time()
    print(f"Eklenme süresi ({size} eleman): {end_time - start_time} saniye")

test_hash_table(200000)
glad frigateBOT
sudden bough
#

well... this won't be very useful because we don't have pypy 🤦‍♂️

cyan nymph
#

Because I'm testing Big O, I use numbers that large. 😅

scenic finch
#

what about this:

import timeit

def test_hash_table(size):
    table = {}
    for i in range(size):
        table[i] = i + 3

print(timeit.timeit("test_hash_table(1000)", globals=globals()))
#
% python bench.py  # python 3.11
42.39127057400037
% pypy3 bench.py            
14.679402190999099
sudden bough
#

Yeah, pypy might just be ignoring this function when it's called only once

scenic finch
#

but the improvement would not be just 3 fold

sudden bough
#

why not?

scenic finch
#

then it would be more than 10s of times

#

AFAIK timeit takes care of that aspect while benching

sudden bough
#

PyPy doesn't speed up all code by 10 times or more, it really depends

scenic finch
#

but if the func is called only once

scenic finch
#

it should be faster

cyan nymph
#

perfect

scenic finch
#

see similar result

sudden bough
#

Try increasingly bigger sizes

scenic finch
#

get me some RAM

sudden bough
#

Something like ```py
import timeit

def test_hash_table(size):
table = {}
for i in range(size):
table[i] = i + 3

for size in [1000, 5000, 10_000, 20_000, 50_000, 100_000, 1_000_000, 2_000_000]:
print(size, timeit.timeit(f"test_hash_table({size})", globals=globals(), number=10_000_000 // size))

scenic finch
#

forgot the fstring in timeit.timeit

sudden bough
#

oh yeah...

#

actually that's still too slow

scenic finch
#

number=1000

#

...is slow too

sudden bough
#

fixed

#
df@duckpond:/tmp$ pyenv local pypy3.10-7.3.15
df@duckpond:/tmp$ python test.py 
1000 0.18167751799990128
5000 0.2896245040000167
10000 0.34291394799993213
20000 0.400657911000053
50000 0.5345783589999655
100000 0.5170535390000168
1000000 0.9001765059999798
2000000 0.9751938440000458
df@duckpond:/tmp$ pyenv local 3.12.3
df@duckpond:/tmp$ python test.py 
1000 0.4880366159999312
5000 0.6853701789999604
10000 0.7441567559999385
20000 0.774592838999979
50000 0.7592545259999497
100000 0.922786970000061
1000000 1.2632231769999862
2000000 1.3310079849999283
df@duckpond:/tmp$ 
scenic finch
#

pypy is faster than cpython

sudden bough
#

It might only activate the JIT when a function runs a certain number of times

#

which is why I suggested fiddling with the JIT flags

cyan nymph
scenic finch
#

surprising:

CPython
% python bench.py
1000 0.038954724001087015
5000 0.2507603809972352
10000 0.5566556570011016
20000 1.1521581019987934
50000 3.1912593839988403
100000 7.569676145001722
1000000 86.05590453100012

Numba
% python bench.py
1000 8.896027073002188
5000 0.08299110499865492
10000 0.16387912899881485
20000 0.5420342799989157
50000 1.1029537720023654
100000 2.3013623080005345
1000000 31.587898476998816

Pypy
% pypy3 bench.py
1000 0.018211006001365604
5000 0.09754729400083306
10000 0.24521737299801316
20000 0.5805304580026132
50000 1.4802869759987516
100000 3.3758814339998935
1000000 65.33701768899846
#

numba is way faster than pypy

cyan nymph
scenic finch
#

smth so wrong with that

#

didn't you copy the new code

cyan nymph
scenic finch
#

paste it

cyan nymph
scenic finch
#

you didnt

cyan nymph
#

ahh sory 👀

#

much much better

scenic finch
#

but coming back to your code

#

i dont think that is the way to benchmark

#

timeit is the way

cyan nymph
#

is it True?

#

Lastest bencmark

scenic finch
#

smth peculiar happens on 1 mil

cyan nymph
#

any idea?

glad frigateBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.