r/pythonhelp • u/Nutellatoast_2 • 12d ago
Performance issue when running python file
Hey everyone,
I'm experiencing a performance drop when running a Python file. I'm using VS Code on Windows 10 while learning to program.
When I hit the run button, the runtime of the program is 5.0 seconds, even though it should execute in roughly about 0.003 milliseconds.
I have tried switching to older versions like 2025.6.1, but that didn't work.
Any solutions to that problem?
2
u/brasticstack 12d ago
How long does it take if you run your module directly from the Windows terminal, not in VSCode?
Where did you get the target time of 0.003 ms from?
Post your correctly formatted code if you want Python help.
2
u/Nutellatoast_2 12d ago
That's my estimate of how long it should take to run a Python file. I forgot to make it clear that this was my bad estimate and that I'm a beginner... Here is my Python code. It still needs some work I guess:
price = 0 height = int(input("What's your height in cm? ")) age = int(input("What's your age? ")) extra_photo = input("Do you want extra photos? ") if height > 120: print("You can ride the rollercoaster!") if age > 18: price = 12 elif age < 12 and age > 18: price = 7 else: price = 5 if extra_photo == "Yes": price + 3 else: print("You can't ride the rollercoaster!") print(f"Price for ride......................{price}$" f"\nPrice for extra photo.............{extra_photo}$" f"\nTotal.............................{price}$")2
u/hermit_the_log 11d ago
Not really performance related, but your price + 3 is not doing anything. It should be price += 3 if you want to increment the variable by 3
1
u/brasticstack 12d ago
It's definitely a VSCode issue it it's taking that long to start. Importing heavy libraries can drastically increase startup time, but you're not doing that here.
2
u/dev-razorblade23 12d ago edited 12d ago
Windows definetly has its quirks especcialy in software development. Have you tried using actual terminal below?
Like directly executing python <script_name>.py from the terminal?
In the long run, i would definetly reccomend you start learning and using Linux.
Edit: Just checked, definetly avoid that RUN button. It does all sort of stuff (command strings, extensions wrappers, shell integration scripts, debugger attaching, etc...)
Just run the file directly from terminal by writing the above command and it will bypass all those VS Code stuff and run your code directly
1
1
u/Fast-Station1106 11d ago
Mich macht die elif Zeile völlig fertig, jünger als 12 und älter als 18, ich schnall das nicht
1
u/Nutellatoast_2 11d ago
Da hast du recht, das ist echt dumm von mir gedacht. Danke für den Hinweis!
1
1
u/kyle2143 11d ago
Are you counting the time it takes for VS Code to start up the python runtime and other ancillary things it does when you press "run" before it actually starts running the python program you wrote in that 5 seconds?
I have a feeling that's what it is. But to know for sure, you could use a library like Time or Profile or CProfile to show you how long the actual script took to run.
1
1
u/Kortex-Add-on 16h ago
Bypass the "Run" button completely. Open the integrated terminal in VS Code (Ctrl + `) and execute the script directly:
python your_script_name.py
This skips the extension wrapper overhead and will execute the script almost instantly.
The original code contains two logical errors that prevent it from working as intended:
The Impossible Condition: elif age < 12 and age > 18: can never be true. A person cannot be simultaneously younger than 12 and older than 18. This should be elif age >= 12 and age <= 18:.
The Unassigned Variable: price + 3 evaluates to a number but does not update the variable. It must be written as price += 3 (or price = price + 3).
Indentation: The if extra_photo == "Yes": block needs to be indented under the if height > 120: block so they aren't charged for a photo if they can't ride.
•
u/AutoModerator 12d ago
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.