# Wolfram Language Speed Guide

Here's a guide to speed up your WolframLang code.

Things discussed here are sensible only if your data is huge, such as list with thousands or millions items. Or, when you are in a coding competition for speed. Some tips sacrifice code convension and readability for speed.

## General Principle

Most important first. The items are increasingly esoteric.

- Avoid any procedural programing such as using
`Do`

,`For`

,`While`

, and lots assignment and variable updates. Doing them is a magnitude slower than using functional programing code. - Do not repeatedly add/delete element to a List. Because WolframLang List is an array in computer science sense. Everytime list length changes, the list is recreated, and that is slow, especially when there are large number of items. Instead, leave the items there as is, extract element in final step. Or, create a large list in the beginning, and change items to some random symbol (e.g x1234) when you don't need them. Or, use SparseArray or Association
- The less number of function calls, the faster. Try to find a WolframLang function that does exactly what you want, instead of using multiple function calls to construct result.
- Use of Pattern Matching is in general an order slower than not using pattern.
This means, when you define a function, best to define it using Function when you can.
However, if you require a one or more
`If`

conditionals to determine branching, pattern matching is usually faster. - Using
`Set`

and`SetDelayed`

, slows down your entire program. [see Set, SetDelayed] because they are global pattern matching, and they are tried for every evaluation of expressions. - When using Pattern Matching on large expression, try to narrow down parts of expression to match, or a level. e.g. use
`ReplaceAt`

at a Position ,`Replace`

at a level, instead of`ReplaceAll`

. - Avoid Patterns that match nested structure or repetition of a complex pattern.
`With`

is faster than`Module`

In the following, most useful tip comes first.

For much less significant speed differences, see Wolfram Language Speed Trivia

## Table vs Append

`Append`

in a loop is mega slow.
The longer the list, the exponentially slower.

$HistoryLength = 1; Module[{datasize, rs1, rs2, tm1, tm2}, datasize = 10000; tm1 = Timing[ rs1 = Table[ 1, {datasize} ]; ]; rs2 = {}; tm2 = Timing[ Do[ AppendTo[ rs2, 1 ], {datasize} ]; ]; Print@ tm1; Print@ tm2; Print@ (rs1 === rs2); ] (* 0.001 0.17 True *)

## Select, Cases, Exit When Found

When using `Select`

, and if you just want first found, give an argument that specifies the max count of item to return.
It basically exit the search when the item is found.
Or use `SelectFirst`

$HistoryLength = 1; Module[{bigdata, rs1, rs2}, bigdata = ReplacePart[ Table[ 0, {1000000} ], 9 -> 1 ]; Print@ First@ Timing[ rs1 = Select[ bigdata, OddQ ]; ]; (* exit when found first *) Print@ First@ Timing[ rs2 = Select[ bigdata, OddQ, 1 ]; ]; Print@ (rs1 === rs2); ] (* 0.187 0.015 True *)

When using `Cases`

, and if you just want first found, give an argument that specifies the max count of item to return.
Or use `FirstCase`

$HistoryLength = 1; Module[{datasize, rs1, rs2, tm1, tm2}, bigdata = RandomInteger[ {0,100}, 1000000 ] ; tm1 = Timing[ rs1 = First@ Cases[ bigdata, 2 ]; ]; (* exit immediately when found *) tm2 = Timing[ rs2 = First@ Cases[ bigdata, 2, {1}, 1 ]; ]; Print@ First@ tm1; Print@ First@ tm2; Print@ (rs1 === rs2); ] (* 0.031 0.015 False *) (* rs1 is twice as slow *)

## Transpose is constant time and instantaneous

`Transpose`

is constant time and instantaneous, regardless of matrix size.
It is a great trick for many situations.

(* Transpose is instantaneous and constant time. regardless of matrix size *) Module[{xx,yy}, xx = Table[ {x, y}, {x, 3}, {y, 3} ]; yy = Table[ {x, y}, {x, 10^3}, {y, 10^3} ]; Print@ First@ Timing[ Transpose[ xx ]; ]; Print@ First@ Timing[ Transpose[ yy ]; ]; ] (* 0 seconds each *)

## Save Repeating Costy Expression to a constant

xtodo need to make better example. result so far puzzling.

if you have a complex expression repeated, use a temp constant for the expression to compute it just once.

For example, you need to check if the length of vector is zero, if so, return zero, else do something with that vector involving that length.

$HistoryLength = 1; Module[{ bigdata, xCostly, f1,f2,time1,time2, rs1, rs2}, (* bigdata = RandomInteger[{-10, 10}, {10, 100000}]; *) bigdata = RandomReal[{-10, 10}, {10, 1000000}]; (* suppose this is slow function *) xCostly = Function[#1 . #1]; (* compute just once, save to a constant *) f1 = Function[With[{x = xCostly[#1] }, If[ x < 0.001 , #1, #1/x] ]]; (* repeated computation *) f2 = Function[If[ (xCostly[#1]) < 0.001 , #1, #1/(xCostly[#1])]]; ClearSystemCache[]; time1 = First@ Timing[ rs1 = f1 /@ bigdata]; ClearSystemCache[]; time2 = First@ Timing[ rs2 = f2 /@ bigdata]; Print@ time1; Print@ time2; Print@ (rs1 === rs2); ] (* result inconclusive *)

(* 2024-02-12 this shows, when using Width, it compute things just once. by adding a Print to function, *) f1 = Function[ Print[ "f1" ]; #1]; f2 = Function[ Print[ "f2" ]; #1]; Function[With[{x = f1[#1] }, If[ x =!= #1 , #1, #1/x] ]][{3,4,5}] Function[If[ (f2[#1]) =!= #1 , #1, #1/(f2[#1])]][{3,4,5}]

## Pure Function vs Pattern Matching Function

Using pattern matching for simple function, such as `f[x_] := `

is
about 20 times slower than using `body``f = Function[`

.
`body`]

(* speed diff, of pattern matching vs Function. 2024-02-11 *) $HistoryLength = 1; Module[{f1,f2,bigdata,tm1,tm2,rs1,rs2}, f1[x_] := x+1; f2 = Function[#+1]; bigdata = RandomInteger[ 100, 1000000 ]; tm1 = Timing[ rs1 = Map[f1, bigdata];]; tm2 = Timing[ rs2 = Map[f2, bigdata];]; Print@ tm1; Print@ tm2; Print@ (rs1 === rs2); ] (* {0.42, Null} {0.01, Null} True *)

## Area vs Handcrafted Triangle Area

the builtin `Area`

function for computing triangle area is some 10 times slower than a hand crafted one.

(* 2024-02-09 the builtin Area function for computing triangle area is some 10 times slower than a hand crafted one *) Module[{TriangleSignedArea, datasize, bigdata,rs1,rs2,tm1,tm2}, TriangleSignedArea[{{x1_, y1_}, {x2_, y2_}, {x3_, y3_}}] := Det[{{x1, y1, 1}, {x2, y2, 1}, {x3, y3, 1}}]/2; datasize = 3000; bigdata = RandomReal[ {-10,10}, {datasize,3,2} ]; tm1 = First@ Timing[ rs1 = Map[ Abs@ TriangleSignedArea @# &, bigdata];]; tm2 = First@ Timing[ rs2 = Map[ Function[Area[ Triangle[ # ] ] ] , bigdata];]; Print@ tm1; Print@ tm2; Print@ (And@@ Flatten@ Map[LessThan[0.00001], Abs@Chop[rs1 - rs2], {-1}]) ] (* 0.015 0.312 True *)

## RegionCentroid vs Handcrafted Centroid for Triangle

(* 2024-02-09 using builtin RegionCentroid to compute triangle centroid, is some 20 times slower *) Module[{TriangleCentroid, datasize, bigdata,rs1,rs2,tm1,tm2}, TriangleCentroid[pts:{{_, _}..}] := Total[ pts ] /Length[pts]; datasize = 3000; bigdata = RandomReal[ {-10,10}, {datasize,3,2} ]; tm1 = First@ Timing[ rs1 = Map[ TriangleCentroid @# &, bigdata];]; tm2 = First@ Timing[ rs2 = Map[ Function[ RegionCentroid@ Triangle@ # ] , bigdata];]; Print@ tm1; Print@ tm2; Print@ (rs1 == rs2); ] (* 0.015 0.265 True *)

## TriangleCenter vs Handcrafted Triangle Center

Using builtin `TriangleCenter`

is some 20 times slower.
Probably because `TriangleCenter`

is an experimental function.

(* 2024-02-09 Speed comparison of computing triangle orthocenter. Using builtin TriangleCenter is some 20 times slower than hand crafted one. *) Module[{TriangleOrthocenter, datasize, bigdata,rs1,rs2,tm1,tm2}, TriangleOrthocenter[{{a1_, a2_}, {b1_, b2_}, {c1_, c2_}}] := {a1*((-b1)*b2 + a2*(b1 - c1) + c1*c2) + (b2 - c2)*(a2^2 + b1*c1 + b2*c2 - a2*(b2 + c2)), -((a1 - b1)*(a1*(b1 - c1) + a2*(b2 - c2)) + (b1 - c1)*((-a1)*c1 + b1*c1 + (-a2 + b2)*c2))}/ (a2*b1 - a1*b2 - a2*c1 + b2*c1 + a1*c2 - b1*c2); datasize = 1000; (* bigdata = Table[ RandomReal[ {-10,10} ], {datasize}, {3}, {2}]; *) bigdata = RandomReal[ {-10,10}, {datasize, 3, 2} ] ; tm1 = First@ Timing[ rs1 = Map[ TriangleOrthocenter, bigdata];]; tm2 = First@ Timing[ rs2 = Map[ Function[ TriangleCenter[#, "Orthocenter" ] ] , bigdata];]; Print@ tm1; Print@ tm2; Print@ (And@@ Flatten@ Map[LessThan[0.00001], Abs@Chop[rs1 - rs2], {-1}]); Print@ TriangleOrthocenter[bigdata[[1]]]; Print@ TriangleCenter[ bigdata[[1]], "Orthocenter" ]; ] (* 0.015 0.171 True {119.15192990444027, 1.4431346540803} {119.15192990444005, 1.4431346540802104} *)