One can make a Named Function (just to have it easily reusable) to measure formula execution using only formulas, no scripts needed.
BENCHMARK()
BENCHMARK()
(returns execution time in seconds):
=N(
REDUCE(
0,
{0; 1; 0},
LAMBDA(
acc,
cur,
IF(
cur = 0,
LAMBDA(x, x)(NOW()) - acc,
acc + 0 * ROWS(func())
)
)
) * 24 * 60 * 60
)
Its single parameter is func
- a function to run and time its execution. You just get a working formula, enclose it in LAMBDA()
and pass to BENCHMARK()
. Examples are at the bottom.
BENCHMARKN()
BENCHMARKN()
:
=N(
REDUCE(
0,
{0; SEQUENCE(number); 0},
LAMBDA(
acc,
cur,
IF(
cur = 0,
LAMBDA(x, x)(NOW()) - acc,
acc + 0 * ROWS(func())
)
)
) * 24 * 60 * 60 / number
)
There's another parameter number
: func
will be executed in a loop number
times. Then total execution time will be divided by number
to get an average execution time.
UPD: enclosing NOW()
in LAMBDA(x, x)(NOW())
freezes the value and prevents recalculation caused by any change in the sheet. Got it from here.
Examples
Say, we want to sum all squares of natural numbers from 1 to 1000000. So we have our formula:
=SUM(ARRAYFORMULA(SEQUENCE(1000000)))
We time it like so:
=BENCHMARK(LAMBDA(SUM(ARRAYFORMULA(SEQUENCE(1000000)^2))))
And in a 10-step loop like so:
=BENCHMARKN(LAMBDA(SUM(ARRAYFORMULA(SEQUENCE(1000000)^2))), 10)
