mikemahoney218 / mm218.dev

Code and assets for my personal website at https://mm218.dev

Home Page:https://mm218.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

posts/2023-08-29-allocations/index

utterances-bot opened this issue · comments

Mike Mahoney - Pre-allocating vectors is for nerds

Or rather: growing objects is inefficient. But it’s maybe not as big a deal as I’d believed.

https://www.mm218.dev/posts/2023-08-29-allocations/index.html

Over on Mastodon, June Choe points to the commits in R associated with this change
https://fosstodon.org/@yjunechoe/110975018204561319

@mikemahoney218 "Always pre-allocate your vectors" is a bit of an outdated dogma. Since v3.4, R already over-allocates memory by a small factor (x1.05) every time a vector needs to grow!

(I learned this from a Hadley tweet a long time ago but can't seem to locate the original discussion)

commented

Hi Mike. I tried an even "devilish" approach using c and unlist and you can see that the results are similar https://gist.github.com/jrosell/ebb604e8c35a8665e2380ef3ea952464

I get the idea from @JosiahParry video about loops https://youtu.be/TdbweYvwnss?si=ho3NKWtBF-KW60HX