In this article, we'll examine the performance difference between approaches for palindrome-products in Python.

The approaches page lists two approaches to this exercise:

  1. Using a nested for loop
  2. Using a nested for loop, optimized edition

Benchmarks

To benchmark the approaches, we wrote a small benchmark application using the timeit library. These tests were run in windows 11, using Python 3.11.1. Your system results may vary.

largest, min=1, max=1000 : 0.05235219991300255
largest_optimized, min=1, max=1000 : 0.000758000067435205
smallest, min=1, max=1000 : 0.04553140001371503
smallest_optimized, min=1, max=1000 : 0.00010269996710121632

largest, min=100, max=100000 : 512.5731259000022
largest_optimized, min=100, max=100000 : 0.013197900028899312
smallest, min=100, max=100000 : 549.5989698000485
smallest_optimized, min=100, max=100000 : 0.03933039994444698

Conclusion

As we can see, the optimized approach is much faster than the original approach. Although the difference becomes most noticeable when the range is large, the optimized approach is still faster in the small range.

23rd Apr 2025 · Found it useful?