原因:timer语句的返回结果是一条消息,不能赋值给一个变量; evalTimer 函数的返回结果是一个标量,可以赋值给变量。
使用evalTimer函数时,需要将统计时间的代码块封装成一个函数,具体使用方法参考下面链接:
https://www.dolphindb.cn/cn/help/FunctionsandCommands/FunctionReferences/e/evalTimer.html
我们可以使用help函数来查看evalTimer函数的参数说明:
help(evalTimer) // evalTimer(funcs, [count=1])
参数funcs:需要统计执行时间的函数。
参数count:是函数执行次数,它是可选参数,默认值为1。
使用示例如下
def tests(){ x=1 } cost = evalTimer(tests,10); print(cost) // 打印结果:0.031341
说明:上述例子将统计时间代码块封装到自定义函数tests中,使用evalTimer函数获取tests函数执行10次的耗时,并将耗时结果赋值给cost变量