简体   繁体   English

C++ 中的执行时间差异(在 function 与按引用传递和按值传递之间)是否显着?

[英]Is the execution time difference (between a function with pass by reference and pass by value) significant in C++?

For Leetcode question 1312,I implemented a pass by value solution and my execution time for a testcase was above 120ms, for the same test case in a pass by reference the execution time drastically reduced to about 8ms, HOW?对于 Leetcode 问题 1312,我实现了 pass by value 解决方案,并且我的测试用例的执行时间在 120ms 以上,对于通过引用传递的相同测试用例,执行时间大幅减少到大约 8ms,如何? Here are both solutions:以下是两种解决方案:

120ms + solution / not accepted: 120ms + 解决方案/不接受:

 class Solution {
public:
    vector< vector<int> > dp;
    int insert(string s,int l,int r)
    {

        if(dp[l][r]!=-1)
            return dp[l][r];
        else if(l>=r)
            return 0;

        if(s[l]==s[r])
            dp[l][r] = insert(s,l+1,r-1)  ;
        else 
            dp[l][r] = 1 + min(  insert(s,l+1,r), insert(s,l,r-1) ) ;

        return dp[l][r];
    }

    int minInsertions(string s) {
        dp.resize(s.length()+1, vector<int> (s.length()+1,-1) );
        return insert(s,0,s.length()-1);
    }
};


~8ms solution: ~8ms 解决方案:

   class Solution {
public:
    vector< vector<int> > dp;
    int insert(string& s,int l,int r)
    {

        if(dp[l][r]!=-1)
            return dp[l][r];
        else if(l>=r)
            return 0;

        if(s[l]==s[r])
            dp[l][r] = insert(s,l+1,r-1)  ;
        else 
            dp[l][r] = 1 + min(  insert(s,l+1,r), insert(s,l,r-1) ) ;

        return dp[l][r];
    }

    int minInsertions(string& s) {
        dp.resize(s.length()+1, vector<int> (s.length()+1,-1) );
        return insert(s,0,s.length()-1);
    }
};

I have a couple of questions:我有一些问题:

  • Why is the difference so significant?为什么差异如此显着?
  • Does it happen only for strings, I mean do primitive/built-in data-types behave in the same way?它只发生在字符串上吗,我的意思是原始/内置数据类型的行为方式是否相同?
  • Would pass by pointer result in the same execution as pass by reference?按指针传递会导致与按引用传递相同的执行吗?
  • Also, according to my understanding of a reference variable, it points to the same address except that it has another name, is this correct?另外,根据我对引用变量的理解,它指向同一个地址,只是它有另一个名字,这是正确的吗?

Thank You.谢谢你。

Is the execution time difference (between a function with pass by reference and pass by value) significant in C++? C++ 中的执行时间差异(在 function 与按引用传递和按值传递之间)是否显着?

It can be significant, and it can be insignificant.它可以很重要,也可以微不足道。 It depends.这取决于。

I implemented a pass by value solution and my execution time for a testcase was above 120ms, for the same test case in a pass by reference the execution time drastically reduced to about 8ms我实现了按值传递解决方案,我的测试用例执行时间超过 120 毫秒,对于按引用传递的相同测试用例,执行时间大幅减少到约 8 毫秒

The result of this experiment demonstrates quite clearly a case where the time difference appears to be significant - although without information about variance of the measurements, we cannot be certain that the results are significant statistically.该实验的结果非常清楚地表明了时间差似乎很显着的情况——尽管没有关于测量方差的信息,我们不能确定结果在统计上是显着的。

Why is the difference so significant?为什么差异如此显着?

You can find out using a profiler.您可以使用探查器进行查找。 Given that the change of the argument to a reference appears to improve the speed significantly, it would be reasonable to guess that most of the time is spent on creating multiple copies of the argument.鉴于将参数更改为引用似乎显着提高了速度,因此可以合理地猜测大部分时间都花在了创建参数的多个副本上。

Does it happen only for strings它只发生在字符串上吗

It doesn't happen only for strings.它不仅仅发生在字符串上。 You'll find that there are other types that are slow to copy as well.您会发现还有其他类型的复制速度也很慢。

I mean do primitive/built-in data-types behave in the same way?我的意思是原始/内置数据类型的行为方式是否相同?

Probably not.可能不是。

How much time does it take to copy an integer?复制一个 integer 需要多少时间? Integers are usually a 1-8 bytes.整数通常是 1-8 个字节。 It takes a about single instruction.它需要大约一条指令。

How much time does it take to copy a string?复制一个字符串需要多少时间? How big even is a string?一根绳子到底有多大? Even sizeof(std::string) is more than the largest integer type on your system.甚至sizeof(std::string)也超过系统上最大的 integer 类型。 Then there is the dynamic array, which may potentially be gigabytes in size.然后是动态数组,它的大小可能是千兆字节。 Copying a gigabyte takes more time than copying 8 bytes.复制 1 GB 比复制 8 个字节需要更多时间。 Even if the string isn't that massive, its copy may involve dynamic allocation.即使字符串不是那么大,它的副本也可能涉及动态分配。 Dynamic allocation is much slower than simply copying an integer.动态分配比简单地复制 integer 慢得多。

Would pass by pointer result in the same execution as pass by reference?按指针传递会导致与按引用传递相同的执行吗?

You can find out by measuring.你可以通过测量来发现。 But I can tell you that yes.但我可以告诉你,是的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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