dynamic array initialized within a short function.
Daniel Keep
daniel.keep.lists at gmail.com
Sun Oct 14 08:12:00 PDT 2007
coxalan wrote:
> ....I dare to repost my last message, now including a non-empty subject line.
>
>
> Hello!
>
> I realized that this code:
>
> int func() {
> int res;
> int[] g;
> g.length = 10;
> foreach(v; g) {
> res += v;
> }
> return res;
> }
>
> void main() {
> for(int i=0; i<10000000; i++) {
> func;
> }
> }
>
> runs about 7 times slower than this one:
>
> class Funcclass {
> int[] g;
>
> this() {
> g.length = 10;
> }
>
> int func() {
> int res;
> foreach(v; g) {
> res += v;
> }
> return res;
> }
> }
>
> void main() {
> auto fc = new Funcclass;
> for(int i=0; i<10000000; i++) {
> fc.func;
> }
> }
>
> The compilation was done with -O -release.
> The difference comes from all the memory-allocating and garbage collecting in the first case.
>
> My question is:
> Is it the default solution to put the function 'func' and the array 'g' as members into a common class?
> Or are there other / more elegant solutions for this?
>
> Thanks!
>
int func(int[] g) {
int res;
foreach(v; g) {
res += v;
}
return res;
}
void main() {
auto g = new int[10];
for(int i=0; i<10000000; i++) {
func(g);
}
}
In general, you should try and do as few allocations as possible. If
you're using an array as "scratch space" -- that is, you don't care
about what's in it before or after the function, only that it's there,
it often help to allocate it earlier, and pass it into the function.
Actually, you could add an overload of func like so:
int func() {
auto g = new int[10];
scope(exit) delete g;
return func(g);
}
Now both approaches work, and you can use whichever is most appropriate.
-- Daniel
More information about the Digitalmars-d-learn
mailing list