+ 1

Why lru cache is taking more time

Hi Refer code below: I could observe that LRU CACHE related code takes more time or almost same time compared to normal function. Why so? Does lru cache guaranteed to work faster? https://sololearn.com/compiler-playground/cx63CT2FJFm7/?ref=app

2nd Oct 2024, 12:38 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
6 Answers
+ 7
Ketan Lalcheta , your cached recursive function fibLatest looks strange when it calls another function called fib in its body. If corrected, the time difference will be enormous. Also, avoid using keywords like 'exec' in function names. https://sololearn.com/compiler-playground/cYoyUyqX2xa1/?ref=app
2nd Oct 2024, 2:46 PM
Vitaly Sokol
Vitaly Sokol - avatar
+ 3
The code you are testing is already extremely fast, so the overhead of the cache is just as high or higher than the overhead of the actual function. If your function had to do something more expensive, such as querying a remote database, then caching would save you more time. Try using something far more time consuming in your test function. Maybe just put a loop and do the same fib calcs 1000 times. See how that goes.
2nd Oct 2024, 1:31 PM
Jerry Hobby
Jerry Hobby - avatar
+ 2
If you set it to 35, it does work. These are the results: 0:00:03.449316 0:00:02.418752 If you set it to 15, These are the results: 0:00:00.000293 0:00:00.000279 If you set it to 2, These are the results: 0:00:00.000003 0:00:00.000003 So you can see that the longer the function takes to run, the better the performance improvement with caching. This proves your code works, but it offers improvement only when the function runs longer.
2nd Oct 2024, 2:15 PM
Jerry Hobby
Jerry Hobby - avatar
0
Thanks Jerry Hobby . Sounds good . Unfortunately, on sololearn Android app, I don't get any output with 100 as number
2nd Oct 2024, 1:54 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Thanks
3rd Oct 2024, 3:44 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Why %d
4th Oct 2024, 4:25 AM
Zohaib Aslam
Zohaib Aslam - avatar