pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <hlinnakangas@vmware.com>
To: M Putz <puma@c007.de>
Cc: pgsql-performance@postgresql.org
Subject: Re: Strange performance boost with random()
Date: Mon, 10 Feb 2014 22:03:36 +0200
Message-ID: <52F93098.2090002@vmware.com> (raw)
In-Reply-To: <alpine.DEB.2.02.1402102007580.14849@skaj>
References: <E1WCttM-0006Np-Nj@malur.postgresql.org>
	<alpine.DEB.2.02.1402102007580.14849@skaj>
List-Unsubscribe:  <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>

On 02/10/2014 09:52 PM, M Putz wrote:
>
> Hello,
>
> While analyzing performance, we encountered the following phenomenon,
>
>     SELECT sum(pow(.5*generate_series,.5))
>     FROM generate_series(1,1000000);
>
> is much much (a hundred times) slower than
>
>     SELECT sum(pow(random()*generate_series,.5))
>     FROM generate_series(1,1000000);
>
> and asymptotic difference is even more astounding.
> This seems counter-intuitive, considering the cost of
> an additional random() call instead of a constant factor.
> What are the reasons for this strange performance boost?

Different data type. The first uses numeric, which is pretty slow for 
doing calculations. random() returns a double, which makes the pow and 
sum to also use double, which is a lot faster.

To see the effect, try these variants:

SELECT sum(pow(.5::float8 * generate_series,.5))
FROM generate_series(1,1000000);

SELECT sum(pow(random()::numeric * generate_series,.5))
FROM generate_series(1,1000000);

- Heikki


-- 
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance



view thread (4+ messages)  latest in thread

Message-ID: <52F93098.2090002@vmware.com>
Permalink:  ../52F93098.2090002@vmware.com/
Also on:    postgresql.org/message-id/52F93098.2090002@vmware.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: hlinnakangas@vmware.com, puma@c007.de
  Subject: Re: Strange performance boost with random()
  In-Reply-To: <52F93098.2090002@vmware.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox