Skip to content
Advertisement

Why is this task faster in Python than Julia?

I ran the following code in RStudio:

JavaScript

It reads a huge NASA CSV file, converts it to a dataframe, converts each element to string, and adds them to a vector.

RStudio took 4 min and 15 seconds.

So I decided to implement the same code in Julia. I ran the following in VS Code:

JavaScript

The result was good. The Julia code took only 1 minute and 25 seconds!

Then for pure curiosity I implemented the same code this time in Python to compare. I ran the following in VS Code:

JavaScript

The result shocked me! Only 35 seconds!!! And in Spyder from Anaconda only 26 seconds!!! Almost 2 million floats!!! Is Julia slower than Python in data analysis? Can I improve the Julia code?

Advertisement

Answer

NOTE: I wrote the below assuming you want the other column order (as in the Python and R examples). It is more efficient in Julia this way; to make it work equivalently to your original behaviour, permute the logic or your data at the right places (left as an exercise). Bogumił’s anwer does the right thing already.


Put stuff into functions, preallocate where possible, iterate in stride order, use views, and use builtin functions and broadcasting:

JavaScript

Which certainly can be optimized harder.

Or shorter, making use of what DataFrames already provides:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement