Python Forum
Confused about python execution
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Confused about python execution
#1
I copy my programs to a directory that is a ram disk (for speed). Note that execution speed is critical. Startup time is not.
cp ~key/Programs.Source/py3*.py ~key/Programs # Copy to RAM disk

Then I execute:
python3 -m compileall ~key/Programs/py3*

This creates the subdirectory __pycache__ and the .pyc files

Now, presently, I execute my programs by having in the bash script:
python3 Programs/[xxx.py]
Is this the fastest way for them to execute or should I be executing them as
python3 Programs/__pycache__/[xxx.pyc]
or simply
Programs/__pycache__[/xxx.pyc]


It was my understanding that if the subdirectory __pycache_ existed, it would automatically use the program name there but I am not too sure any more.

My objective is to have them execute as quickly as possible.

Also, at the time I originally built the programs, I did not use
#/usr/bin/python3
because I built them with python2 knowing I would eventually go over to python3. I never got around to adding in that line.
Is there any significant difference in time to use that line and do this:
Programs/[xxx.py]
instead of this
#/usr/bin/python3 Programs/[xxx.py]
or is it just good practice so as to identify that this is a Python3 script vs any other type and that the shell knows to call /usr/bin/python3 to run it? Even 40 years ago, I knew to use the #!/bin/sh header but didn't do it for my python programs.
(Note, there was no bash - only sh and csh back in those days)
Reply
#2
(Oct-07-2022, 04:02 PM)jpezz Wrote: My objective is to have them execute as quickly as possible.
Usually, when a program is slow, the bottleneck is not the time it takes to load the program in memory. The actions that you are suggesting such as storing the program in RAM or using compiled Python files only alter the time that it takes to load the program. It will not accelerate the algorithms or the execution of the Python code.

The only case I can think of where this could change things is if the program does almost nothing and the loading time becomes significant with respect to the real execution time.

Compiled Python files .pyc are automatically used when a module such as «spam» is imported and the spam.pyc file is more recent than the spam.py file. Normally you don't need to worry about this.

Here is an example on my computer with a program «pyspam.py» that simply prints «Hi» and does nothing else. As you can see the running time is about the same for the .py file, the .pyc file and the .pyc file copied to ramdisk.
Output:
λ time python3 paillasse/pf/pyspam.py hi real 0m0,048s user 0m0,032s sys 0m0,016s λ time python3 paillasse/pf/__pycache__/pyspam.cpython-310.pyc hi real 0m0,050s user 0m0,046s sys 0m0,004s λ cp paillasse/pf/__pycache__/pyspam.cpython-310.pyc /media/ramdisk λ time python3 /media/ramdisk/pyspam.cpython-310.pyc hi real 0m0,049s user 0m0,040s sys 0m0,008s
jpezz likes this post
Reply
#3
Thanks for your reply. I see a notable difference in my program speeds when using ram disks for program execution and for storing output information (which is copied to the real disk periodically) but that's not the real subject of my question.

Using your example, it appears that executing
python3 paillasse/pf/pyspam.py

is notably slower than
python3 paillasse/pf/__pycache__/pyspam.cpython-310.pyc

but it is not clear that in the first example that you created the pyc file before the first example. To clarify my question, if the pyc file exists AND is newer AND I specify the py file, does python use the pyc such that the two commands take the same time and it uses the pyc automatically meaning I don't have to explicitly use it in the command line or should I be using the pyc file by name as shown in the second example? Your example clearly shows the extra benefit of the pyc file but it is not clear as to whether it existed prior to running the py file or whether you created it between those two examples.

I am also curious as to whether using the use of the
Quote:#/usr/bin/python3
so as to directly call the program speeds anything up vs having to specify
Quote: python3 program.py
.


My guess is that pre-compiling and creating the pyc file removes all the comment lines (I have LOTS of them mostly to help me remember what and why I was doing when I wrote each line of the code), merging the includes into the code and doing the interpretation ahead of time to increase the execution speed and reduce the loading time.

My guess is also that the only purposes of the shebang is to tell the executing program (i.e. python, python2, python3) and the person running it that code being executed is indeed really intended for that compiler.
Reply
#4
The shebang more or less is for nix os. Useless for windows os
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
(Oct-09-2022, 03:54 PM)jpezz Wrote: I see a notable difference in my program speeds when using ram disks for program execution and for storing output information
If your program does Input/Output, it will be faster if you read and write from RAM, but storing the program itself in RAM is not necessary, there is very little benefit. The program is always loaded to RAM when it is executed.
(Oct-09-2022, 03:54 PM)jpezz Wrote: Your example clearly shows the extra benefit of the pyc file
It does not. If the program runs during more than a few seconds, the difference is neglectible. Try with real programs and show the performance measurements.
(Oct-09-2022, 03:54 PM)jpezz Wrote: #/usr/bin/python3
The shebang line starts with #! , not # . It does not speed up anything.
(Oct-09-2022, 03:54 PM)jpezz Wrote: if the pyc file exists AND is newer AND I specify the py file, does python use the pyc
No, as far as I know. Python uses automatically the .pyc file when modules are imported for the first time. I don't think it does this for the main program.
(Oct-09-2022, 03:54 PM)jpezz Wrote: My guess is also that the only purposes of the shebang is to tell the executing program
This guess is wrong. In Linux (and Unixes in general), if the file has a shebang and it is executable, the OS uses the shebang to know which program to call (here python3).

Again, apart from Input/Output in RAM, which can be really useful, all these «optimizations» seem futile to me.
Larz60+ likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  kill python execution program lebossejames 0 246 Mar-16-2024, 11:16 AM
Last Post: lebossejames
  C++ programmer confused about why Python isn't working the way I intend Radical 2 729 Sep-15-2023, 04:21 AM
Last Post: Radical
  String int confused janeik 7 1,084 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  I am confused with the key and value thing james1019 3 972 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Pandas confused DPaul 6 2,569 Sep-19-2021, 06:45 AM
Last Post: DPaul
  is and '==' i'm confused hshivaraj 6 2,719 Sep-15-2021, 09:45 AM
Last Post: snippsat
  Confused with 'flags' tester_V 10 4,923 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Simple Tic Tac Toe but I'm confused Izith 1 2,197 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  I am really confused with this error. Runar 3 3,027 Sep-14-2020, 09:27 AM
Last Post: buran
  How to to tie the execution of one process to another inside a loop in Python ignorant_wanderer 0 2,054 Jul-11-2020, 03:44 AM
Last Post: ignorant_wanderer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020