0

I overheard that running compiled .pyc can speed up execution, in contrast to running raw .py. But in my little experiment, it turns little or no improvement:

# hello.py

import pandas as pd
import numpy as np
import time


if __name__ == '__main__':

    loop = 100000

    print('Hello World')
    start = time.time()
    for l in range(loop):
        df = pd.DataFrame([[np.random.uniform(0,np.random.uniform(0,1)),np.random.uniform(0,np.random.uniform(1,10))],
                        [np.random.uniform(0,np.random.uniform(10,100)),np.random.uniform(0,np.random.uniform(100,1000))]],index=[0,1],columns=['A','B'])
    print(f'Time cost for {loop} iteration: {time.time()-start:.4f} sec')
# (bash) python hello.py
Hello World
Time cost for 100000 iteration: 28.2506 sec
# (bash) python -O -m compileall -b .
python hello.pyc
Hello World
Time cost for 100000 iteration: 26.9610 sec

Specification:

Python 3.9.13

[GCC 11.2.0] :: Anaconda, on linux (Redhat, centOS)

Can .pyc really cut runtime in general, or just for some cases, or no effect at all? Or am I missing some conducts?

Jack Lee
  • 73
  • 5

0 Answers0