测量电子表格内置函数的执行时间

时间:2017-10-25 04:00:43

标签: google-apps-script google-sheets profiling

是否有方法可以在为电子表格完成内置函数时测量执行时间?当我使用几个内置函数(例如,IMPORTHTML和IMPORTXML)时,如果我知道平均执行时间,我很容易使用和设计数据表。

我使用此脚本测量自定义函数。

function myFunction() {
  var start = new Date();

  // do something

  var end = new Date();
  var executiontime = end - start;
}

非常感谢您的时间和建议。

3 个答案:

答案 0 :(得分:4)

不幸的是,没有用于检索内置函数执行时间的测量工具。这已经被@Rubén评论过了。所以我想到了解决方法。以下解决方法怎么样?

流程:

  1. 将值导入单元格。这个值是好的,因为它被用作触发器。请自己做。
    • 自定义功能无法使用setValue()。所以我使用了onEdit()
  2. func1()导入您希望通过触发器启动的脚本测量执行时间的公式。
  3. func2(),设置公式后,开始测量。内置功能完成时的确认使用循环执行。
    • 通过衡量getValue()每次通话的费用,发现大约是0.0003秒。所以我认为可以使用它。
  4. 测量结果可以在Stackdriver中看到毫秒。
  5. 示例脚本:

    function func1(range, formula){
      range.setFormula(formula);
    }
    
    function func2(range){
      var d = range.getValue();
      while (r == d) {
        var r = range.getValue();
      }
    }
    
    function onEdit(){
      var formula = '### Built-in function ###'; // Please set the built-in function you want to measure the execution time.
    
      var label = "Execution time for built-in functions.";
      var ss = SpreadsheetApp.getActiveSheet();
      var cell = ss.getActiveCell();
      var range = ss.getRange(cell.getRow(), cell.getColumn());
      func1(range, formula);
      console.time(label);
      func2(range);
      console.timeEnd(label);
    }
    

    注意:

    • 当测量具有很长时间的内置功能时,getValue()可能会发生错误。
      • 在我的环境中,10秒内置函数运行良好。

    如果这对你有用,我很高兴。

答案 1 :(得分:2)

Google表格不包含用于衡量重新计算时间的内置工具。

另一种方法是使用Chrome Developers Tools Timeline,但请记住每次电子表格执行时都不会重新计算IMPORTHTML和IMPORTXML等函数(参考Set a spreadsheet’s location and calculation settings)。

相关Q& A

SO

Web应用程序

答案 2 :(得分:0)

要添加其他选项,您可以执行以下操作:

function myFunction() {
  console.time('someFunction');

  // do something

  console.timeEnd('someFunction');
}

然后查看Stackdriver日志中的函数执行。

示例输出为:

Jul 3, 2020, 1:03:00 AM Debug someFunction: 80ms