我正在运行此 question 中显示的代码.我希望它第二次和第三次运行得更快(第一次运行需要时间来编译代码)。但是,它似乎花费了与第一次相同的时间。我怎样才能使这段代码运行得更快?
编辑:我通过在 Linux 终端上给出命令来运行代码:julia mycode.jl
我尝试按照@Przemyslaw Szufel 的回答中的说明进行操作,但出现以下错误:
julia> create_sysimage(["Plots"], sysimage_path="sys_plots.so", precompile_execution_file="precompile_plots.jl")
ERROR: MethodError: no method matching create_sysimage(::Array{String,1}; sysimage_path="sys_plots.so", precompile_execution_file="precompile_plots.jl")
Closest candidates are:
create_sysimage() at /home/cardio/.julia/packages/PackageCompiler/2yhCw/src/PackageCompiler.jl:462 got unsupported keyword arguments "sysimage_path", "precompile_execution_file"
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
Stacktrace:
[1] top-level scope at REPL[25]:1
我在 Debian 稳定版 Linux 上使用 Julia:Debian ⛬ julia/1.5.3+dfsg-3
最佳答案
在 Julia 中,包在每次使用单个 Julia session 运行时都会被编译。因此,启动一个新的 Julia 进程意味着每次 Plots.jl
都会被编译。这是一个相当大的包,因此需要花费大量时间进行编译。
为了绕过它,使用PackageCompiler
和compile Plots.jl into a static system image that can be used later by Julia
基本步骤包括:
using PackageCompiler
create_sysimage(["Plots"], sysimage_path="sys_plots.so", precompile_execution_file="precompile_plots.jl")
完成此操作后,您需要将代码运行为:
julia --sysimage sys_plots.so mycode.jl
同样,您可以将 MultivariateStats
和 RDatasets
添加到生成的 sysimage 中,但我认为它们不会造成任何重大延迟。
请注意,如果后续运行是您开发过程的一部分(而不是您的生产系统实现),并且您是,例如。开发 Julia 模块比您更愿意考虑在开发过程中使用 Revise.jl
而不是预编译 sysimage。基本上,拥有 sysimage 意味着您每次更新 Julia 包时都需要重建它,因此我会考虑将这种方法用于生产而不是开发(取决于您的具体情况)。
关于performance - Julia 似乎很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73599900/