4 ms·
An excellent example. This is also one of those things that is hard to profile away, because it essentially leads to a very flat profile because you're probably
by lomnakkus 11y ago
An excellent example. This is also one of those things that is hard to profile away, because it essentially leads to a very flat profile because you're probably forgetting to use with_capacity()/reserve() all over the place not just in single very-costly cases[1]. So you end up wasting, say, 1% CPU time all over the place... add up enough of these time-wasters and soon you're going to be wasting significant amounts of CPU time for no good reason.
Obviously, this kind of thing isn't a magic bullet, but in general I'd say the key for any kind of program is to express your intent at the highest level possible. This benefits both optimization and human readers! (That obviously means that the programming language must support such abstraction in a non-leaky way.)
[1] Which would show up prominently on a profile.
- tbrownaw 11y agoNot really... the wasted time is still all malloc() / realloc() / whatever under array.push(), and a half-decent callgraph profiler will catch that. array.push() will score far higher than it ought to, and the bulk of its time will be the reallocation. Source: I've used oprofile, and later perf, to hunt exactly this kind of performance bug. . Of course, fixing a pervasive issue like that is much more tedious than just finding it. :-(