Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 测量将参数传递给函数所需的时间_C++_Time_Arguments - Fatal编程技术网

C++ 测量将参数传递给函数所需的时间

C++ 测量将参数传递给函数所需的时间,c++,time,arguments,C++,Time,Arguments,有没有办法通过传递参数来测量函数花费了多少时间?我的想法是测量函数的主体和整个函数本身。这是正确的方法吗下面的伪代码: 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

有没有办法通过传递参数来测量函数花费了多少时间?我的想法是测量函数的主体和整个函数本身。这是正确的方法吗下面的伪代码:

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);
}
intmain(){
int start=time.now();
//按值传递
int timeOfFunc=foo(向量大向量,向量大向量2);
int end=time.now();
int timespontpassingargs=(end-start)-timeOfFunc;//获取传递参数所需的时间?
}
int foo(向量大向量,向量大向量2)
{
int start=time.now();
//foo函数的主体
int end=time.now();
返回(结束-开始);
}

首先,我建议打印调用函数的汇编语言。这将使您了解传递变量所需的工作

为了获得有意义的概要文件,您应该测量函数调用前的时间和调用后的时间,并在1E6迭代中执行。然后可以获得调用函数所需的平均执行时间

另一种方法是查找函数调用中使用的每个汇编指令所需的时钟周期。这可能不准确,因为处理器执行汇编语言指令的方式(它可能并行执行,代码可能在指令缓存中,也可能不在指令缓存中,等等)

编辑1:示波器

测量性能的好工具是示波器。在嵌入式系统领域,代码写入测试点(或LED)。例如,LED将在函数调用之前打开,在函数调用之后关闭。将示波器探头连接到LED上。然后可以使用o'scope测量LED亮起的持续时间。同样,需要执行多次迭代才能获得更好的平均值。

我看不出您的方法有任何错误,只是计时将包括在函数中调用两次
time.now()
所需的时间。这应该是相当小的,虽然相比成本的复制

您可以尝试的另一个选项如下(使用伪代码):


这应该告诉你调用函数和复制参数需要多长时间。我不确定编译器是否允许/可以优化出未使用的参数副本,但如果允许,您应该得到一个零。

foo(vector-hugh\u-vector,vector-hugh\u-vector2)
这不是调用函数的方式。为什么需要测量传递参数所需的时间?如果你想知道复印一份需要多长时间,那就只需要时间复印一份。否则,只需通过引用/const-reference/rvalue引用传递“大”对象,并且不要开始复制。@raket1111因此短语“下面的伪代码”@WhozCraig没有看到伪代码,你是对的。我以前从未见过有人通过计时进行调试。听起来你应该对这个问题做一个分析,然后问为什么这不能代替这个。
function(parameters)
{
    return time.now();
}

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