简体   繁体   中英

Measure time it takes to pass the parameters to the function

Is there a way to measure how much time the function has spent by passing the parameters? My thought is to measure the body of the function and the whole function itself. Is that the right approach? Pseudo code below:

int main(){
  int start = time.now();
  // passing by value
  int timeOfFunc = foo(vector<my_object> huge_vector, vector<my_object2> huge_vector2);
  int end = time.now();
  int timeSpentPassingArgs = (end - start) - timeOfFunc; // getting time it takes to pass the argument?
}

int foo(vector<my_object> huge_vector, vector<my_object2> huge_vector2)
{
 int start = time.now();
 // body of the foo function
 int end = time.now();
 return (end - start);
}

Firstly, I recommend printing the assembly language of the function making the call. This should give you an idea of the work required to pass the variables.

In order to have a meaningful profile, you should measure the time before the function call and the time after and perform over 1E6 iterations. You can then get an average of the execution time required to call a function.

Another alternative is to look up the clock cycles required by each assembly instruction used in the function call. This may not be accurate due to how the processor executes the assembly language instructions (it may perform in parallel, the code may or may not be in an instruction cache, etc.).

Edit 1: oscilloscopes
A good tool to measure performance is an oscilloscope. In the Embedded Systems area, the code writes to a test point (or LED). For example, the LED would be turned on before the function call and turned off after the function call. An oscillocope probe would be attached to the LED. The o'scope can then be used to measure the duration that the LED is on. Again, many iterations would need to be performed to get a better average.

I can't see anything wrong with your approach except that you timings will include the time it takes to call time.now() twice in the function. That should be pretty minor though compared to the cost of the copying.

Another option you can tries follows like (using pseudo code):

function(parameters)
{
    return time.now();
}

// calling code
time start = time.now();
time end = function(paramters);
time total_time = end - start;

And that should tell you exactly how long it takes to just call the function and copy the parameters. I'm not sure if the compiler is allowed to/can optimized out an unused copy of the parameters but if it does you should just get a zero.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM