[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Performance optimization (allocation inside a for loop)
From: |
Jaroslav Hajek |
Subject: |
Re: Performance optimization (allocation inside a for loop) |
Date: |
Thu, 2 Apr 2009 07:12:41 +0200 |
On Thu, Apr 2, 2009 at 1:18 AM, r <address@hidden> wrote:
> This is a general problem. It doesn't just affect scripting languages
> - you may hit this issue equally well in C/C++.
>
> A common strategy is to allocate a bit more space than currently
> needed, so that malloc/realloc doesn't need to be called every single
> time the user appends data to the structure. Even better, this margin
> can be made dependent on the current structure size (e.g. proportional
> to n or to log(n)). This second scheme results in better
> performance/memory occupation ratio.
>
> I'm (now) perfectly OK with preallocating memory manually, but it took
> me a lot of time to figure out where the bottleneck is. BTW, low "for"
> loop performance is commonly blamed on the lack of a JIT compiler, but
> I wouldn't be surprised if at least some of the test cases were
> actually suffering from issues like memory allocation. After all, JIT
> compilers don't typically speed up evaluation by more than a constant
> factor.
>
> Regards,
> -r.
>
I don't think it would be hard to implement a chunked allocation in
Octave, though it surely would complicate the array internals
somewhat. But I see little need for it. The preferred coding style in
Octave is using vectorized expressions, where you normally don't face
this issue.
Also, the choice of a good allocation strategy is subtle. To reduce
the reallocation time in your loop to O(log(N)), you need to
over-allocate by a multiplicative factor, which may actually make
things worse for someone dealing with large arrays.
--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz
- Re: Performance optimization (allocation inside a for loop), (continued)
- Re: Performance optimization (allocation inside a for loop), Elias Assmann, 2009/04/02
- Re: Performance optimization (allocation inside a for loop), James Sherman Jr., 2009/04/02
- Re: Performance optimization (allocation inside a for loop), John W. Eaton, 2009/04/02
- Re: Performance optimization (allocation inside a for loop), Elias Assmann, 2009/04/02
- Re: Performance optimization (allocation inside a for loop), Rob Mahurin, 2009/04/02
- Re: Performance optimization (allocation inside a for loop), John W. Eaton, 2009/04/02
- Re: Performance optimization (allocation inside a for loop), Rob Mahurin, 2009/04/02
- Re: Performance optimization (allocation inside a for loop), Jaroslav Hajek, 2009/04/02
- Re: Performance optimization (allocation inside a for loop), Jaroslav Hajek, 2009/04/02
- Re: Performance optimization (allocation inside a for loop), Francesco Potorti`, 2009/04/02
- Re: Performance optimization (allocation inside a for loop),
Jaroslav Hajek <=
- Re: Performance optimization (allocation inside a for loop), Andreas Romeyke, 2009/04/02
Re: Performance optimization (allocation inside a for loop), Przemek Klosowski, 2009/04/03
Re: Performance optimization (allocation inside a for loop), Francesco Potorti`, 2009/04/02
- Re: Performance optimization (allocation inside a for loop), r, 2009/04/02
- Re: Performance optimization (allocation inside a for loop), John W. Eaton, 2009/04/02
- Re: Performance optimization (allocation inside a for loop), r, 2009/04/02
- Re: Performance optimization (allocation inside a for loop), Jaroslav Hajek, 2009/04/04
- Re: Performance optimization (allocation inside a for loop), r, 2009/04/04
- Re: Performance optimization (allocation inside a for loop), Jaroslav Hajek, 2009/04/04