Skip to content
Advertisement

Torch: Why is this collate function so much faster than this other one?

I have developed two collate functions to read in data from h5py files (I tried to create some synthetic data for a MWE here but it is not going to plan).

The difference between the two in processing my data is about 10x — a very large increase and I am unsure as to why and I am curious for insights for my future collate functions.

JavaScript

I thought the one below, with more loops, would be slower, but it is far from the case.

JavaScript

Edit: I tried swapping to this: It still is about 10x slower compared to ‘fast’.

JavaScript

Advertisement

Answer

See this answer (and give it an upvote): https://stackoverflow.com/a/30245465/10475762

Particularly the line: “In other words and in general, list comprehensions perform faster because suspending and resuming a function’s frame, or multiple functions in other cases, is slower than creating a list on demand.”

So in your case, you’re calling append multiple times each collate, which is called quite a few times in your training/testing/evaluation steps which all adds up. IMO, always avoid for loops whenever you can as it seems to somehow invariably lead to slowdowns.

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