[Python-Dev] Caching float(0.0)
Kristján V. Jónsson
kristjan at ccpgames.com
Fri Sep 29 18:11:25 CEST 2006
Well gentlemen, I did gather some stats on the frequency of PyFloat_FromDouble().
out of the 1000 first different floats allocated, we get this frequency distribution once our server has started up:
- stats [1000]({v=0.00000000000000000 c=410612 },{v=1.0000000000000000 c=107838 },{v=0.75000000000000000 c=25487 },{v=5.0000000000000000 c=22557 },...) std::vector<entry,std::allocator<entry> >
+ [0] {v=0.00000000000000000 c=410612 } entry
+ [1] {v=1.0000000000000000 c=107838 } entry
+ [2] {v=0.75000000000000000 c=25487 } entry
+ [3] {v=5.0000000000000000 c=22557 } entry
+ [4] {v=10000.000000000000 c=18530 } entry
+ [5] {v=-1.0000000000000000 c=14950 } entry
+ [6] {v=2.0000000000000000 c=14460 } entry
+ [7] {v=1500.0000000000000 c=13470 } entry
+ [8] {v=100.00000000000000 c=11913 } entry
+ [9] {v=0.50000000000000000 c=11497 } entry
+ [10] {v=3.0000000000000000 c=9833 } entry
+ [11] {v=20.000000000000000 c=9019 } entry
+ [12] {v=0.90000000000000002 c=8954 } entry
+ [13] {v=10.000000000000000 c=8377 } entry
+ [14] {v=4.0000000000000000 c=7890 } entry
+ [15] {v=0.050000000000000003 c=7732 } entry
+ [16] {v=1000.0000000000000 c=7456 } entry
+ [17] {v=0.40000000000000002 c=7427 } entry
+ [18] {v=-100.00000000000000 c=7071 } entry
+ [19] {v=5000.0000000000000 c=6851 } entry
+ [20] {v=1000000.0000000000 c=6503 } entry
+ [21] {v=0.070000000000000007 c=6071 } entry
(here I omit the rest).
In addition, my shared 0.0 double has some 200000 references at this point.
0.0 is very, very common. The same can be said about all the integers up to 5.0 as well as -1.0
I think I will add a simple cache for these values for Eve.
something like:
int i = (int) fval;
if ((double)i == fval && i>=-1 && i<6) {
Py_INCREF(table[i]);
return table[i];
}
Cheers,
Kristján
> -----Original Message-----
> From: python-dev-bounces+kristjan=ccpgames.com at python.org
> [mailto:python-dev-bounces+kristjan=ccpgames.com at python.org]
> On Behalf Of Kristján V. Jónsson
> Sent: 29. september 2006 15:18
> To: Fredrik Lundh; python-dev at python.org
> Subject: Re: [Python-Dev] Caching float(0.0)
>
> Acting on this excellent advice, I have patched in a reuse
> for -1.0, 0.0 and 1.0 for EVE Online. We use vectors and
> stuff a lot, and 0.0 is very, very common. I'll report on
> the refcount of this for you shortly.
>
> K
>
> > -----Original Message-----
> > From: python-dev-bounces+kristjan=ccpgames.com at python.org
> > [mailto:python-dev-bounces+kristjan=ccpgames.com at python.org]
> > On Behalf Of Fredrik Lundh
> > Sent: 29. september 2006 15:11
> > To: python-dev at python.org
> > Subject: Re: [Python-Dev] Caching float(0.0)
> >
> > Nick Craig-Wood wrote:
> >
> > > Is there any reason why float() shouldn't cache the value
> > of 0.0 since
> > > it is by far and away the most common value?
> >
> > says who ?
> >
> > (I just checked the program I'm working on, and my analysis
> tells me
> > that the most common floating point value in that program
> is 121.216,
> > which occurs 32 times. from what I can tell, 0.0 isn't
> used at all.)
> >
> > </F>
> >
> > _______________________________________________
> > Python-Dev mailing list
> > Python-Dev at python.org
> > http://mail.python.org/mailman/listinfo/python-dev
> > Unsubscribe:
> > http://mail.python.org/mailman/options/python-dev/kristjan%40c
> cpgames.com
> >
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/kristjan%40c
cpgames.com
>
More information about the Python-Dev
mailing list