Re: Strange plpgsql performance -- arithmetic, numeric()

From: Pavel Stehule <stehule(at)kix(dot)fsv(dot)cvut(dot)cz>
To: "Karl O(dot) Pinc" <kop(at)meme(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Strange plpgsql performance -- arithmetic, numeric()
Date: 2005-04-03 22:59:51
Message-ID: Pine.LNX.4.44.0504040050390.13204-100000@kix.fsv.cvut.cz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> Incrementing the loop counter by a factor of 10, from 1000 to 10000
> makes the process take more than 100 times longer. (I only saw
> this happen when I happened upon using a numeric() datatype
> and then dividing i/100 to avoid overflow. It does not happen
> without the array and working with other, much larger, arrays
> of other data types you see no slowdown.)
>
It's not bug, it's feature ;-). plpgsql isn't good language for
inicialisation big arrays. If it's possible use plperl for example.

CREATE OR REPLACE FUNCTION speed1(integer) RETURNS numeric(7,2)[] AS $$
$i = 0.00;
@myarray = ();
while ($i<$_[0]) {
push @myarray, $i;
$i = $i + 1;
}
return '{'.join(',',@myarray).'}';
$$ LANGUAGE plperlu;
select speed(100);

CREATE OR REPLACE FUNCTION speed2(integer) RETURNS numeric(7,2)[] AS $$
DECLARE a numeric(7,2)[] = '{}';
BEGIN
FOR _i IN 1..$1 LOOP
a[_i] := _i;
END LOOP;
RETURN a;
END;
$$ LANGUAGE plpgsql;

tarif=# select speed(10000);
Time: 28,269 ms

tarif=# select speed2(10000);
Time: 91186,199 ms

Regards
Pavel Stehule

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Pavel Stehule 2005-04-03 23:46:06 Re: Strange plpgsql performance, diff plperl and plpgsql
Previous Message Keary Suska 2005-04-03 22:50:01 Re: Empty date