Re: vector::push_ba ck performance
Method Man wrote:[color=blue]
> [...]
> Your analogies didn't really help in my understanding of realloc.[/color]
They were intended to show the pitfalls of using 'realloc' with generic
types. Since now we're talking specifically POD, they are moot.
[color=blue]
> I was
> looking for something like -- 'realloc' is never/sometimes/always more
> efficient than malloc'ing a new array and manually copying from the old
> array (of PODs). Then justify the choice.[/color]
'realloc' works in an implementation-defined way. There is always some
possibility that a memory block allocated for an array can be simply
extended without the need to copy. There is always some possibility
that mere calling malloc and then some kind of copying (even memcpy)
can be less efficient when it's done from within your code than if it
is done in the library written (and optimised) specifically for your
hardware. So, in general 'realloc' will _always_ be at least as fast
as you can emulate it with your own 'malloc' and 'memcpy'.
More on C standard library - in comp.lang.c.
Victor
Method Man wrote:[color=blue]
> [...]
> Your analogies didn't really help in my understanding of realloc.[/color]
They were intended to show the pitfalls of using 'realloc' with generic
types. Since now we're talking specifically POD, they are moot.
[color=blue]
> I was
> looking for something like -- 'realloc' is never/sometimes/always more
> efficient than malloc'ing a new array and manually copying from the old
> array (of PODs). Then justify the choice.[/color]
'realloc' works in an implementation-defined way. There is always some
possibility that a memory block allocated for an array can be simply
extended without the need to copy. There is always some possibility
that mere calling malloc and then some kind of copying (even memcpy)
can be less efficient when it's done from within your code than if it
is done in the library written (and optimised) specifically for your
hardware. So, in general 'realloc' will _always_ be at least as fast
as you can emulate it with your own 'malloc' and 'memcpy'.
More on C standard library - in comp.lang.c.
Victor
Comment