I thought about this: Is there a performance difference in these two practices:
Assuming all classes and functions are written correctly.
ClassA a = function1();
ClassB b = function2(a);
function3(b);
function3(function2(function1()));
I know there aren't a big difference with only one run, but supposed that we could run this a lot of times in a loop, I created some tests.
#include <iostream>
#include <ctime>
#include <math.h>
using namespace std;
int main()
{
clock_t start = clock();
clock_t ends = clock();
// Case 1.
start = clock();
for (int i=0; i<10000000; i++)
{
double a = cos(1);
double b = pow(a, 2);
sqrt(b);
}
ends = clock();
cout << (double) (ends - start) / CLOCKS_PER_SEC << endl;
// Case 2.
start = clock();
for (int i=0; i<10000000; i++)
sqrt(pow(cos(1),2));
ends = clock();
cout << (double) (ends - start) / CLOCKS_PER_SEC << endl;
return 0;
}
Why is the first one is much slower, and if the second one is faster why dont we always write code that way? Anyway does the second pratice has a name?
I also wondered what happens if I create the variables outside the for loop in the first case, but the result was the same. Why?
We can declare Temp Variables only using a Declare statement but Temp Tables can be created using Create Table and Select Into commands. We cannot drop a Temp variable but Temp Tables can be dropped using a Drop Command. We cannot use the truncate command for Temp Variables but we can do it for Temp Tables.
In computer programming, a temporary variable is a variable with short lifetime, usually to hold data that will soon be discarded, or before it can be placed at a more permanent memory location. Because it is short-lived, it is usually declared as a local variable, i.e., a variable with local scope.
A return value can be any one of the four variable types: handle, integer, object, or string. The type of value your function returns depends largely on the task it performs.
In C++, a function which is defined as having a return type of void , or is a constructor or destructor, must not return a value. If a function is defined as having a return type other than void , it should return a value.
Break the throw-this-all-away optimization if you want the computational crunch and your numbers become much more consistent. Ensuring the code to get the proper value is actually run and not entirely thrown out, I've assigned the results in both tests to a volatile local (which isn't exactly proper usage of volatile, but does a decent job of ensuring only the value-creation is the significant delta).
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
int main()
{
clock_t start;
volatile double val;
for (int j=1;j<=10;j++)
{
// Case 1.
start = clock();
for (int i=0; i<2000000; i++)
{
double a = cos(1);
double b = pow(a, 2);
val = sqrt(b);
}
cout << j << ':' << (double) (clock() - start) / CLOCKS_PER_SEC << endl;
// Case 2.
start = clock();
for (int i=0; i<2000000; i++)
val = sqrt(pow(cos(1),2));
cout << j << ':' << (double) (clock() - start) / CLOCKS_PER_SEC << endl << endl;
}
return 0;
}
Produces the following release-compiled output on my Macbook Air (which is no speed demon by any stretch):
1:0.001465
1:0.001305
2:0.001292
2:0.001424
3:0.001297
3:0.001351
4:0.001366
4:0.001342
5:0.001196
5:0.001376
6:0.001341
6:0.001303
7:0.001396
7:0.001422
8:0.001429
8:0.001427
9:0.001408
9:0.001398
10:0.001317
10:0.001353
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With