An example of improving the performance of Fibonacci sequence by using Redis caching

Source: Internet
Author: User
Tags redis
Go to a company interview, PHP post, first written, then two rounds of interviews, the last HR face, took an offer.




The written question is not difficult, finished to take a picture, and then go back to the Fibonacci sequence of the problem on the computer run again, it's certainly true, but when you ask for Nth, when N is greater than 60 it will be very slow, and then you want to use Redis cache to optimize performance, the effect is very amazing!







I changed the title, and I wanted to use recursion, but I wanted to list the Fibonacci numbers from 0 to N and cache them with Redis.




The idea is very simple, every time to take the value of the nth Redis there is no, there will be no need to calculate, not on the calculation of a save, greatly reduce the number of calculations. The deposit is a hash structure. the code is as follows:





To write a controller method in the Laravel framework:



Fibonacci Series
Public Function fib ($n = null)
{
if ($n > 100) {
Die (' number must <= 100! ');
}
for ($i = 0; $i <= $n; $i + +) {
echo $this->getnum ($i). ' ';
}
}

Private Function Getnum ($n)
{
$key = ' Com.tanteng.me.test.foo ';
$value = Redis::hget ($key, $n);
if ($value) {
return $value;
}
if ($n = = 0) $result = 1;
if ($n = = 1) $result = 1;
if ($n > 1) {
$result = $this->getnum ($n-2) + $this->getnum ($n-1);
}
Redis::hset ($key, $n, $result);
Redis::expire ($key, 1800);
return $result;
}



To add a route to the routes.php file:



Php




Route::get ('/test/fib/{n?} ', [' Uses ' => ' TESTCONTROLLER@FIB ']);
Access via browser: HTTP://WWW.111CN.NET/TEST/FIB/65



The results came soon:



1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811-5142 29 832040 1346269 2178309, 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494 437 701408733 1134903170 1836311903 2971215073 4807526976 7778742049 12586269025 20365011074 32951280099 53316291173-8626 7571272 139583862445 225851433717 365435296162 591286729879 956722026041 1548008755920 2504730781961 4052739537881 6557470319842 10610209857723 17167680177565 27777890035288








If you do not need to Redis, the 65 Fibonacci sequence will be very slow, the use of Redis is almost a brush to come out. In actual development, Redis is also widely used, Redis is really a good thing


Related Article

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.