Per Møldrup-Dalum

Quickly, run a Wolfram script

If one loves to use the Wolfram Language to do computational stuff, one might be a bit annoyed at how long a time the runtime takes to execute a script.


 >: time wolframscript -code "Now"
DateObject[{2026, 4, 2, 22, 42, 0.125742`5.852055352936617}, Instant, Gregorian, 2.]
wolframscript -code "Now"  0,07s user 0,05s system 5% cpu 2,193 total

 >: time python3 -c "from datetime import datetime; print(datetime.now())"
2026-04-02 22:56:38.775207
python3 -c "from datetime import datetime; print(datetime.now())"  0,02s user 0,01s system 77% cpu 0,045 total
        

Of course, one might argue that that's not what the Wolfram Language is intended for, and one might be right. But still, why not?

So, if one would like a method to quickly run a small code snippet or script, here is one such solution.

Start a terminal and run this command:


$ wstpserver
        

This starts the Wolfram Symbolic Transfer Protocol Server or WSTP Server (See IntroductionToWSTPServer for more) and gives one a persistent Wolfram kernel that one can send expressions to for execution. E.g.,


 >: time wolframscript -wstpserver -code "Now"
DateObject[{2026, 4, 2, 22, 53, 42.092027`8.376774818009682}, Instant, Gregorian, 2.]
wolframscript -wstpserver -code "Now"  0,01s user 0,01s system 8% cpu 0,249 total
        

This gave us a factor 10 speedup which is good enough for a lot of tasks, especially considering the advantages that the Wolfram Language gives.

Still, this can be improved a bit. If one reads deeper in the above references page, one can make the two following aliases


 >: alias wl-init
wl-init='wolframscript -wstpserver -startprofile ${HOME}/wstp-profile -code "Print[\"WSTP server initialized\"];"'

 >: alias wl
wl='wolframscript -wstpserver -continueprofile /Users/au15929/wstp-profile -code'
        

w-init creates a persistent WSPT environment, and wl sends expressions to that persistent environment. Now, one can declare a variable in one wl "a=12" and access it in a following wl "a*13". Quite nifty.

One can have one alias more


 >: alias wlf
wlf='wolframscript -wstpserver -continueprofile /Users/au15929/wstp-profile -file'
        

that instead of an expression, sends a file to be executed in the persistent environment.

Caveats and Future Work

The worst problem in the daily life of this solution is that the current working directory of the persistent environment is where the wstpserver program was started. Mostly, this is ${HOME}. Therefore, it is not easy to access files in an arbitrary working directory without doing the extra work of defining that explicitly. This might get fixed one day.

Another issue is stdin and stdout, that is not available to the envorinment, but that one would like to use all the time when working on the command line. This might also be fixed one day.