3
$\begingroup$

Even a simple

#!/usr/bin/env wolframscript
(* ::Package:: *)
Print[1]

takes 3 seconds to finish on my computer.

Can Mathematica script load only what it needs for executing script? This loading time is so slow and unusable to me as a script language for small job compares to python.

$\endgroup$
7
  • $\begingroup$ Do you use the engine? That takes longer to start up for the first time, most certainly for the license check. The engine uses a license that expires every month, and every invocation with an expired license renews the license, and every subsequent launch then happens instantly. So if you use the engine, just try it again, and if it's not the engine, i. e. a locally installed M, this shouldn't happen. $\endgroup$ Commented Feb 1, 2024 at 2:40
  • $\begingroup$ @AndreasLauschke Thank you for reply. I do not use engine. I am using windows mma, executing xxx.wls. No matter how many times I run xxx.wls, it always takes 4 seconds. $\endgroup$ Commented Feb 1, 2024 at 3:11
  • $\begingroup$ Get a real operating system then. There are soooooooooo many Linux flavors. It's not on the M side of things. M comes up just fine and fast on a real operating system, I use the scripting functionality several times every day. Try deleting the .Mathematica directory in your user directory (will be re-created), try deleting the accumulated stuff in the temp directory--Win loves to accumulate the temp gunk until it is manually deleted, on Linux the temp dir is in RAM, that's must faster and disappears/resets on every boot. $\endgroup$ Commented Feb 1, 2024 at 3:31
  • $\begingroup$ And the usual on Win: try disabling anti-spy, incremental backup, VPN, ... is scripted M the only thing that launches so slowly? How do other scripts come up? $\endgroup$ Commented Feb 1, 2024 at 3:33
  • 1
    $\begingroup$ @AndreasLauschke Well, actually I have a linux server. Running mma script is only a little faster, takes about 1.7 seconds. So I am really curious that you say you can execute script instantly. $\endgroup$ Commented Feb 1, 2024 at 11:12

1 Answer 1

3
$\begingroup$

If you are willing to use only a subset of the language (excluding all those symbols defined in top-level code), you may use the undocumented '-runfirst' switch. In Windows:

PS C:\Users\Glenn> Measure-Command {math -run '2+2;Exit[]'}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 2
Milliseconds      : 200
Ticks             : 22000335
TotalDays         : 2.54633506944444E-05
TotalHours        : 0.000611120416666667
TotalMinutes      : 0.036667225
TotalSeconds      : 2.2000335
TotalMilliseconds : 2200.0335


PS C:\Users\Glenn> Measure-Command {math -runfirst '2+2;Exit[]'}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 132
Ticks             : 1322163
TotalDays         : 1.53028125E-06
TotalHours        : 3.672675E-05
TotalMinutes      : 0.002203605
TotalSeconds      : 0.1322163
TotalMilliseconds : 132.2163

I discovered this while trying to solve the same problem as you. I actually used it to implement a faster script reader. Just remember that using '-runfirst' restricts the language somewhat. I don't think even NIntegrate works at this stage, unfortunately.

$\endgroup$
4
  • 1
    $\begingroup$ Thank you so much! Wow, this is fast! wolframscript -runfirst "Print[Names[]];Exit[]" shows a lot of functions, that is great. But unfortunately, it seems that it -runfirst does not support script file. $\endgroup$ Commented Feb 1, 2024 at 11:10
  • 1
    $\begingroup$ If you do something like this: math -noprompt -runfirst '<<script.txt;Exit[]' Then it works. You just have to manually call Print any time you want a line outputted. I personally found that kind of annoying and spent some time re-implementing Get ('<<') at the top level to print every line automatically. I can share some of my code if you'd find it helpful. $\endgroup$ Commented Feb 1, 2024 at 17:49
  • $\begingroup$ To be clear, I've added the Mathematica directory to the system PATH variable and I'm invoking math.exe directly, instead of wolframscript.exe, so I'm not sure if things are different that way. $\endgroup$ Commented Feb 1, 2024 at 17:50
  • $\begingroup$ Wow, thank you so much! This almost solves my issue. By almost I mean, I do not understand why Mathematica doesn't provide this execution mode explicitly and force us to use undocumented -runfirst combined with awkward <<script;Exit[] : ) $\endgroup$ Commented Feb 2, 2024 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.