我的Python程序在编译之前运行得更快。

huangapple go评论82阅读模式
英文:

My python program works faster before compiling it

问题

Hello I have a program that I wrote. It reads an excel file and inserts it into sqlite. It is like a CLI. It works very well on the terminal. But after I compile it with pyinstaller, it took 1m20s to read and write 16 rows of an excel file. On VSCode (or without compiling it), it takes a few seconds.

Is it about pyinstaller? I tried with and without --onefile.

Since it has other functions, it is relatively large to post here.

英文:

Hello I have a program that I wrote. It reads en excel file and inserts it into sqlite. It is like a CLI. It works very well on terminal. But after I compile it with pyinstaller It took 1m20s to read and write 16 rows of excel file. On VSCode (or without compiling it) it takes few seconds.

Is it about pyinstaller? I tried with and without --onefile.

Since it has other functions It is relatively large to post here.

答案1

得分: 4

以下是要翻译的内容:

问题可能在于对使用PyInstaller的目的有一些误解,以及它能够实现什么(以及不能实现什么):

PyInstaller并不会将您的代码编译成类似于优化的机器代码,以比常规Python代码更快的速度运行(这是“编译”一词可能会暗示的)。相反,它的目的是为您的代码提供可移植性:PyInstaller将您的代码与运行所需的所有依赖项捆绑到一个文件夹(默认)或文件中(使用PyInstaller的--onefile参数)。从某种意义上说,它提供了一个可移植的最小Python环境来运行您的代码 - 请参阅《PyInstaller的功能和工作原理》1

特别是使用--onefile选项时,这意味着捆绑的代码必须首先解压缩,然后才能运行。这又意味着,与直接在Python终端或IDE中运行相比,从PyInstaller捆绑包中运行的小段代码将慢得多,这正是由于这种解压缩开销。

*)是的,它可以与您的代码的字节码版本一起工作(即*.pyc文件),但这些文件也是由您的Python终端或IDE生成的,因此在运行时不应有任何区别(您可以在此讨论中详细了解字节码文件的作用)。

英文:

The problem lies probably in a misconception of what using PyInstaller does (and does not) achieve:

PyInstaller does not compile your code into anything like optimized machine code that is supposed to run faster than regular Python code (which is what the term "compile" might imply)*. Instead, its purpose is to provide portability for your code: PyInstaller wraps up your code together with all the dependencies that are needed to run it into a single folder (default) or file (using PyInstaller's --onefile argument). In a sense, it provides a portable minimum Python environment to run your code – see What PyInstaller Does and How It Does It.

Especially with --onefile, this means that the bundled code has to be extracted first before it can be run. This, in turn, means that a small snippet of code will be much slower when run from a PyInstaller bundle than when run directly in your Python terminal or IDE, precisely due to this extraction overhead.

*) Yes, it works with bytecode versions of your code (i.e. *.pyc files), but these are also produced by your Python terminal or IDE and thus should not make a difference in running time (you can read more on the role of bytecode files in this discussion).

答案2

得分: -1

问题出在pyinstaller中。如果想直接运行脚本,请在bash中编写一个运行您的脚本的脚本。

英文:

The issue is in pyinstaller.
if want run the script directly write script in bash that runs your

huangapple
  • 本文由 发表于 2023年7月18日 13:58:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定