python programming language courses
Image: Images: Depositphotos

Python is the most preferred programming language for Machine Learning and Artificial Intelligence, but it is also the least preferred for being slow to solve certain problems that involve loops.

To challenge this fact, researchers at EPFL Computer Vision Laboratory published a report in which they presented the competitiveness of Python against C++ and Go by solving the popular N-queens puzzle.

Comparing Python, C++ and Go in solving N-Queens problem

The N-queens problem involves an arrangement of the N-queens on the NxN chessboard such that no queens can strike any other queen (no two queens can come in the same horizontal, vertical, or diagonal way).

The researchers solved the N-queens puzzle using both sequential and parallel methods written in C++, Python, and Go. They argued that although C++ wins the race followed by Go, Python also comes closer once compiled using the Numba compiler.

For demonstrating the result, they took the standard 8×8 and 10×10 board to solve and run all versions of code on 2.9GHz Quad-Core Intel Core i7 on Catalina OS.

They also reran all code on an Intel Xeon X5690 CPU running Ubuntu 18.04, and the overall ranking of the implementations remained unchanged.

Sequential and Parallel Processing result

A recursive algorithm is the best method to compute the N-queen problem completely. The solution relied on the fact that no two queens be in conflict with each other as they must not be on the same row, on the same column, or on the same diagonal.

In sequential processing, implementation in Python, Go, and C++ indicated that Python was the slowest. But when the same code was compiled using the Numba Python compiler, C++ still won with Go slower by 6% and Numba Python slower by 12%.

They concluded that C++ and Go, being closer to the hardware, may be useful to fine-tune code under some circumstances where Go can be considered an alternative to both Python and C++, because of its run-time check.

To further implement Python, Go, and C++ using parallel processing, they categorized Python into Para and Pool based on two approaches to run Python on a multicore CPU.

The result demonstrated that in parallelization, Para Numba-compiled code displays significant improvement and comes closer to the C++ and Go. Moreover, Python succeeds Go for larger values, but, C++ is still faster.

Overall, the study shows that the slowness of Python can largely be overcome by using the Numba compiler that delivers performance comparable to that of C++, which itself is faster than Go.