ModularM
Modular2y ago
9 replies
Martin Dudek

Python integration and performance

I am looking into the performance of the Python Integration in Mojo. I use Dict here as example but that is just random, my question is not about a `Dict but in general

The following python program measures
time: 0.585089921951294 sec

on my computer to fill and modify a dictionary. as follows
import time
NUM = 1_000_000
start = time.time()
dic = {}
for i in range(NUM):
    dic[str(i*2)] = i%3
for i in range(NUM):
    dic[str(i*2)] *= 2
elapsed = (time.time()-start)
print("time:",elapsed,"sec")

When I include the dict into Mojo the performance drops significantly
time: 15.87300 sec

from python import Python
from time import now
alias NUM = 1_000_000
fn main() raises:
    var start = now()
    var dict = Python.dict()
    for i in range(NUM):
        dict[str(i*2)] = i%3
    for i in range(NUM):
        dict[str(i*2)] *=2 
    var elapsed = (now()-start)/1_000_000_000 
    print("time:",elapsed,"sec")
    _ = dict["112"]

Now when i shift the first loop into a python program
def get_dict(num):
    dict = {}
    for i in range(num):
        dict[str(i*2)] = i%3
    return dict
`
and use this in Mojo as follows:
from python import Python
from time import now
alias NUM = 1_000_000
fn main() raises:
    start = now()
    Python.add_to_path("./utils")  
    var utils: PythonObject = Python.import_module("utils")
    var dict = utils.get_dict(NUM)
    for i in range(NUM):
        dict[str(i*2)] *= 2
    var elapsed = (now()-start)/1_000_000_000 
    print("time:",elapsed,"sec")
    _ = dict["112"]

i get
time: 11.133032 sec

which is 1,5 times faster.

What I am mainly wondering about now are the last 2 examples. If performance is crucial, is it in certain cases when we need to rely on Python Integration advisable to perform some calculations directly in Python instead of just importing the Python object to Mojo, It feels odd but here it brings speedup.

Thanks for any thoughts on that.
Was this page helpful?