Review "newly discovered" 2007-10-30 16:32

Hey, the impulse is the devil. I thought this was what it found, in fact, is a very simple truth, but we do not think it up. When bored today, think of their operational efficiency. Which found the exchange to achieve two multiplication, subtraction can. Hey, is found at the beginning so excited carefully think about is actually very common, for his next. lets change a topic.

See how their efficiency.

#include <windows.h>
#include <iostream>

#pragma comment(lib,"Kernel32.lib")

int main()
{
 int a = 3,b = 6,c = 0;
 DWORD start = 0,end = 0;
 start = GetTickCount();
 a = a + b;
 b = a - b;
 a = a - b;
 end = GetTickCount();
 c = end - start;
 std::cout<<"c is: "<<c<<std::endl;
 std::cout<<"a is: "<<a<<std::endl;
 std::cout<<"b is: "<<b<<std::endl;
 std::cout<<"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"<<std::endl;
 start = GetTickCount();
 a = a * b;
 b = a / b;
 a = a / b;
 end = GetTickCount();
 c = end - start;
 std::cout<<"c is: "<<c<<std::endl;
 std::cout<<"a is: "<<a<<std::endl;
 std::cout<<"b is: "<<b<<std::endl;
 std::cout<<"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"<<std::endl;
 start = GetTickCount();
 a = a ^ b;
 b = a ^ b;
 a = a ^ b;
 end = GetTickCount();
 c = end - start;
 std::cout<<"c is: "<<c<<std::endl;
 std::cout<<"a is: "<<a<<std::endl;
 std::cout<<"b is: "<<b<<std::endl;
 system("pause");
 return 0;
}

Code like this. The resulting output is:

c is: 0
a is: 6
b is: 3
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
c is: 0
a is: 3
b is: 6
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
c is: 0
a is: 6
b is: 3

 

The same efficiency. Ha ha.

 

 

You cheated, in fact, efficiency is not the same. When they translate into assembly you will find ^ back to the most complex operations, but it is less than a microsecond operation, the system is ignored, so we see the result is always zero. GetTickCount Gets the number of milliseconds since.

Guess you like

Origin www.cnblogs.com/lu-ping-yin/p/10988569.html