I am copying a dictionary and with every time I do so it takes longer

Viewed 67

I need a dictionary for a function and I noticed that with every time I create/copy it it takes longer.

Here's my code:

empty_dict = {key: [[] for _ in range(days)] for key in keys}

for _ in range(iterations):
    now = datetime.datetime.now()
    signal_dict = msgpack.unpackb(msgpack.packb(empty_signal_dict))
    print(f'{(datetime.datetime.now() - now).total_seconds()}s')

    other_stuf()

The output is:

1.309264s
1.167535s
2.710208s
9.595995s
12.05153s 
9.190252s 
9.815069s 
16.377981s 
19.699576s 
26.775421s 
36.03728s 
35.815498s 
178.561634s 
150.163415s

You can maybe see why this is an issue. The weird thing is that I tried to change my copying function, but it will always take longer with every iteration. I tried creating the dictionary every time, loading it with json/pickle and using copy.deepcopy(). Has anyone an idea what's the problem here. The rest of my script doesn't has this problem, only this part.

0 Answers
Related