mikemahoney218 / mm218.dev

Code and assets for my personal website at https://mm218.dev
https://mm218.dev
Other
25 stars 14 forks source link

posts/2023-08-29-allocations/index #19

Open utterances-bot opened 10 months ago

utterances-bot commented 10 months ago

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

mikemahoney218 commented 10 months ago

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)

jrosell commented 10 months ago

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