Hacker News new | ask | show | jobs
by fiedzia 11 days ago
> Typically, doing things like changing algorithms, data structures, careful use of caching, reusing calculations, etc. are more important to performance than the choice of language.

The choice of language determines the extent to which those things are possible. In case of Python using C libraries sometimes means that you pull data from somewhere, hold them in Python memory in very inefficient way, pay for transformation to another form that C library uses and then do the same in reverse at the end. Perhaps even more times if C doesn't handle everything. "Use C libraries" often works, but is not universal solution.

1 comments

As always, it depends.

For example, PyTorch (and numpy IIUC) store matrices/tensors as a 1D array of numbers. This is more memory efficient as all the data is colocated.

They use a technique to store the distance within the array to move when moving along each axis (strides). This allows for operations such as transpose to be swapping the array and stride values instead of performing an expensive array/vector copy.

Another example: using string formatter or join instead of separate concatenations allows the compiler/language to compute the size of the resulting string up front (if the language/library supports that) rather than allocating on demand.

Another example: using a hash map/set instead of an array to check for the presence of strings.