class_docstr: Evaluate metrics for y_true and y_pred.
class_docstr_tok: ["Evaluate","metrics","for","y_true","and","y_pred","."]
docstr: Return the time cost in milliseconds of a specific function by running multiple times.
  
  :param func: The function to be tested for latency.
  :param args: arguments for the tested function.
  :param num_running: Int and the value is positive. Specify the running number of
         the function and the value defaults to 100.
  :param kwargs: other arguments for the tested function.
  
  :return: Dictionary of str:float.
           Show the information of the time cost in milliseconds.
  
  Example:
      >>> # to get the inferencing performance of a trained TCNForecaster
      >>> x = next(iter(test_loader))[0]
      >>> # run forecaster.predict(x.numpy()) for len(tsdata_test.df) times
      >>> # to evaluate the time cost
      >>> latency = Evaluator.get_latency(forecaster.predict, x.numpy(),                num_running = len(tsdata_test.df))
      >>> # an example output:
      >>> # {"p50": 3.853, "p90": 3.881, "p95": 3.933, "p99": 4.107}
docstr_tok: ["Return","the","time","cost","in","milliseconds","of","a","specific","function","by","running","multiple","times",".",":","param","func",":","The","function","to","be","tested","for","latency",".",":","param","args",":","arguments","for","the","tested","function",".",":","param","num_running",":","Int","and","the","value","is","positive",".","Specify","the","running","number","of","the","function","and","the","value","defaults","to","100.",":","param","kwargs",":","other","arguments","for","the","tested","function",".",":","return",":","Dictionary","of","str",":","float",".","Show","the","information","of","the","time","cost","in","milliseconds",".","Example",":",">",">",">","#","to","get","the","inferencing","performance","of","a","trained","TCNForecaster",">",">",">","x","=","next","(","iter","(","test_loader",")",")","[","0","]",">",">",">","#","run","forecaster.predict","(","x.numpy","(",")",")","for","len","(","tsdata_test.df",")","times",">",">",">","#","to","evaluate","the","time","cost",">",">",">","latency","=","Evaluator.get_latency","(","forecaster.predict",",","x.numpy","(",")",",","num_running","=","len","(","tsdata_test.df",")",")",">",">",">","#","an","example","output",":",">",">",">","#","{","``","p50","''",":","3.853",",","``","p90","''",":","3.881",",","``","p95","''",":","3.933",",","``","p99","''",":","4.107","}"]
code: def get_latency(func, *args, num_running=100, **kwargs):
      """
          Return the time cost in milliseconds of a specific function by running multiple times.
  
          :param func: The function to be tested for latency.
          :param args: arguments for the tested function.
          :param num_running: Int and the value is positive. Specify the running number of
                 the function and the value defaults to 100.
          :param kwargs: other arguments for the tested function.
  
          :return: Dictionary of str:float.
                   Show the information of the time cost in milliseconds.
  
          Example:
              >>> # to get the inferencing performance of a trained TCNForecaster
              >>> x = next(iter(test_loader))[0]
              >>> # run forecaster.predict(x.numpy()) for len(tsdata_test.df) times
              >>> # to evaluate the time cost
              >>> latency = Evaluator.get_latency(forecaster.predict, x.numpy(),\
                  num_running = len(tsdata_test.df))
              >>> # an example output:
              >>> # {"p50": 3.853, "p90": 3.881, "p95": 3.933, "p99": 4.107}
          """
      invalidInputError(
          isinstance(num_running, int),
          "num_running type must be int, "
          f"but found {type(num_running)}.",
      )
      if num_running < 0:
          invalidInputError(
              False,
              "num_running value must be positive, "
              f"but found {num_running}.",
          )
  
      time_list = repeat(
          lambda: func(*args, **kwargs), number=1, repeat=num_running
      )
      sorted_time = np.sort(time_list)
  
      latency_list = {
          "p50": round(1000 * np.median(time_list), 3),
          "p90": round(1000 * sorted_time[int(0.90 * num_running)], 3),
          "p95": round(1000 * sorted_time[int(0.95 * num_running)], 3),
          "p99": round(1000 * sorted_time[int(0.99 * num_running)], 3),
      }
  
      return latency_list
  
file_path: BigDL/python/chronos/src/bigdl/chronos/metric/forecast_metrics.py
imports: from numpy import ndarray
  import numpy
  from bigdl.nano.utils.common import invalidInputError
  from timeit import repeat
  import random
label_desc: Use image node_id 2 for calling the Evaluator obj's underlying member method code with example usage: obj.get_latency(func) and returns: latency_list