I’d like to measure and capture my script’s execution time and assign it to an variable. I tried to assign the return of function timer to variable a, but it raised an exception of “invalid expression”.
a = timer x=1
I’d like to measure and capture my script’s execution time and assign it to an variable. I tried to assign the return of function timer to variable a, but it raised an exception of “invalid expression”.
a = timer x=1
The exception is raised because timer is not a function but a procedure. It only prints a message that cannot be assigned to a variable. However, you can use function evalTimer, which returns a scalar that can be assigned to a variable.
To use function evalTimer, you can rewrite the statement block as a user-defined function. See evalTimer — DolphinDB 1.3 documentation
You can also check the syntax of a function with syntax:
syntax(evalTimer)
// evalTimer(funcs, [count=1])
funcs: the function(s) for which the execution time is measured
count: the number of times funcs will be executed. The default value is 1.
To measure the execution time of function foo:
def foo(){
x=1
}
a= evalTimer(foo,10);
print(a)
// output: 0.031341
In the above example, the code to be executed is encapsulated into a user-defined function foo. Then use function evalTimer to measure the time of running function foo 10 times, assign the result to variable a, and print it out.