Algorithm learning-recursion and non-recursion, bit operation and multiplication speed comparison

Source: Internet
Author: User
Tags bitwise gcd

    • Recursive invocation of non-recursive calls
      • Run time comparison
      • Conclusion
    • Bit operation and multiplication method
      • Conclusion

Recursive call/non-recursive invocation

As we all know, many algorithms are implemented recursively. Of course they can also be implemented with non-recursive return.

In general, when we traverse a two-fork tree, and when we seek Fibonacci numbers, recursion is very simple. The code is easy to understand and good to implement.

But when it comes to recursion, there is a problem that requires a stack. Why do you want to press the stack? Because when I call myself inside the function, I want to break the current operation and continue to jump to the next implementation, and the state of the current run will be saved. So we put the current state on the stack and wait until the end of the recursive condition to run, then reload the stack.

So recursion is the need for space!

Run time comparison

Let me compare the recursive and non-recursive comparisons of the elapsed time with a simpler algorithm.

First look at the following code:

////Main.cpp//Euclidean////Created by Alps on 15/3/28.//Copyright (c) 2015 Chen. All rights reserved.//#include <iostream>#include <time.h>using namespace STD;intgcdintAintb) {if(A%b = =0) {returnb }returnGCD (b, a%b);}intgcd_t (intAintb) {inttemp = A; while(A%b! =0) {a = B;        b = temp%b;    temp = A; }returnb;}intMinmultiple (intAintb) {return(a*b)/GCD (A, b);}intMainintargcConst Char* argv[]) {intA = -, B = -;inti =100000;DoubleStart = (Double) clock (); for(intj =0; J < I;    J + +) {gcd (A, b); }DoubleEnd = (Double) clock ();printf("%f\n", End-start); Start = (Double) clock (); for(intj =0; J < I;    J + +) {gcd_t (A, b); } end = (Double) clock ();printf("%f\n", End-start);return 0;}

The comparison here is gcd() very familiar, is the Euclidean algorithm. Do not understand this algorithm can see my previous blog: Euclid algorithm (the method of division of the Euclidean)

In this code, I call the recursive method and the non-recursive method 100,000 times respectively. Not too much, and almost enough.

Paste the results:

3516.0000004077.000000Programwithexit0

You can see the recursive method of time: 3516ms and the Recursive method 4077ms .

Is that a recursive approach that is faster than a non-recursive one?
Personally feel not necessarily.
Why is it? Because I think it might be that the editor is optimized, so it's faster. And when I tested it myself, I tested only a few of the most common ones.

Conclusion

So here's the conclusion:

Non-recursive merging is not necessarily faster than recursion.

However, non-recursion does not require the use of stack space.

Bit operation and multiplication method

Bit operation is relatively simple, generally 与或非( & | ~ ) , there is the 异或( ^ ) last 位移 ( >> << ) two operations.

Because compared with the multiplication method, only >> << these two operations are used here.

Look at the following code:

////Main.cpp//Test////Created by Alps on 14-10-7.//Copyright (c) 2014 Chen. All rights reserved.//#include <iostream>#include <time.h>using namespace STD;intMainintargcChar**ARGV) {intA =1234567;intTime =100000;DoubleStart = (Double) clock (); for(intI=0; i<time;++i) {a = a <<1; }DoubleEnd = (Double) clock ();printf("==========\n bit operate:%f\n==========\n", End-start); A =1234567; Start = (Double) clock (); for(intI=0; i<time;++i) {a = a *2; } end = (Double) clock ();printf("==========\n multiple operate:%f\n==========\n", End-start); Start = (Double) clock (); for(intI=0; i<time;++i) {a = a >>1; } end = (Double) clock ();printf("==========\n bit operate:%f\n==========\n", End-start); A =1234567; Start = (Double) clock (); for(intI=0; i<time;++i) {a = A/2; } end = (Double) clock ();printf("==========\n division operate:%f\n==========\n", End-start);return 0;}

It is easy to see that the purpose of my writing is to output the time of the bitwise operation and the multiplication method.

The results are as follows:

? /Users/alps/Documents/code/c/test/test >./main========== bit operate: 272.000000==================== multiple operate: 268.000000==================== bit operate: 277.000000==================== division operate: 1156.000000==========
Conclusion

As you can see, the multiplication, 10w times, is actually a little bit faster than the bitwise operation. My personal guess is that the compiler optimizes multiplication at the bottom.

but!!
I am not sure TT because I have not read the compiled code, you forgive me. When I see it, I'll put it in detail.

But in the division, it is obvious, because the bit operations are much faster.

Multiplication is a little bit faster than a bitwise operation.
Division is fast without bit arithmetic.

These conclusions are just the result of my running here.

Algorithm learning-recursion and non-recursion, bit operation and multiplication speed comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.