我正在运行此问题中显示的代码。我希望它第二次和第三次运行得更快(第一次运行需要时间来编译代码)。然而,它似乎花费了与第一次相同的时间。我怎样才能让这段代码运行得更快?
\n编辑:我通过在 Linux 终端上发出命令来运行代码:julia mycode.jl
我尝试按照 @Przemyslaw Szufel 的答案中的说明进行操作,但出现以下错误:
\njulia> create_sysimage(["Plots"], sysimage_path="sys_plots.so", precompile_execution_file="precompile_plots.jl")\n\nERROR: MethodError: no method matching create_sysimage(::Array{String,1}; sysimage_path="sys_plots.so", precompile_execution_file="precompile_plots.jl")\nClosest candidates are:\n create_sysimage() at /home/cardio/.julia/packages/PackageCompiler/2yhCw/src/PackageCompiler.jl:462 got unsupported keyword arguments "sysimage_path", "precompile_execution_file"\n create_sysimage(::Union{Array{Symbol,1}, Symbol}; sysimage_path, project, precompile_execution_file, precompile_statements_file, incremental, filter_stdlibs, replace_default, base_sysimage, isapp, julia_init_c_file, version, compat_level, soname, cpu_target, script) at /home/cardio/.julia/packages/PackageCompiler/2yhCw/src/PackageCompiler.jl:462\nStacktrace:\n [1] top-level scope at REPL[25]:1\n
Run Code Online (Sandbox Code Playgroud)\n我在 Debian Stable Linux 上使用 Julia:Debian \xe2\x9b\xac julia/1.5.3+dfsg-3
\n在 Julia 中,包每次在单个 Julia 会话中运行时都会进行编译。因此,启动一个新的 Julia 进程意味着每次都会Plots.jl
被编译。这是一个相当大的包,因此需要花费大量时间来编译。
为了规避它,请使用Plots.jlPackageCompiler
并将其编译为静态系统映像,供 Julia 稍后使用
基本步骤包括:
using PackageCompiler
create_sysimage(["Plots"], sysimage_path="sys_plots.so", precompile_execution_file="precompile_plots.jl")
Run Code Online (Sandbox Code Playgroud)
完成此操作后,您将需要运行代码:
julia --sysimage sys_plots.so mycode.jl
Run Code Online (Sandbox Code Playgroud)
MultivariateStats
同样,您可以将和添加RDatasets
到生成的 sysimage 中,但我认为它们不会造成任何明显的延迟。
请注意,如果后续运行是您的开发过程的一部分(而不是您的生产系统实现)并且您是例如。开发 Julia 模块比您更愿意考虑Revise.jl
在开发过程中使用而不是预编译 sysimage。基本上,拥有 sysimage 意味着您每次更新 Julia 软件包时都需要重建它,因此我会考虑这种方法,而不是用于生产而不是开发(取决于您的具体场景)。
我遇到了这个问题,几乎又回到了 Python,但现在我使用 include 在 REPL 中运行脚本。这样要快得多。
注意:首次运行会很慢,但即使编辑了脚本,同一 REPL 会话中的后续运行也会很快。
Fedora 36、朱莉娅 1.8.1