[ http://issues.apache.org/jira/browse/LUCENE-467?page=comments#action_12357911 ]
Yonik Seeley commented on LUCENE-467: ------------------------------------- Here is a new version that's faster by keeping the mantissa and exponent together. It's fast-path does a single shift and a single add after getting the float bits. public byte floatToByte(float f) { int bits = Float.floatToRawIntBits(f); int smallfloat = bits >> 21; // only keep 3 highest bits in mantissa if (smallfloat < (63-15)<<3) { return (bits<=0) ? (byte)0 : (byte)1; // 0 or underflow } if (smallfloat >= ((63-15)+32)<<3) { return -1; // overflow } return (byte)(smallfloat - ((63-15)<<3)); } ---JVM--- -CUR-- -NEW-- -DIFF- 14-server 75.422 66.515 13% 14-client 89.451 79.734 12% 15-server 8.344 3.859 116% 15-client 57.631 17.031 238% 16-server 7.656 3.172 141% 16-client 57.984 16.531 251% These numbers include the overhead of a float loop and the method call overhead. > Use Float.floatToRawIntBits over Float.floatToIntBits > ----------------------------------------------------- > > Key: LUCENE-467 > URL: http://issues.apache.org/jira/browse/LUCENE-467 > Project: Lucene - Java > Type: Improvement > Components: Other > Versions: 1.9 > Reporter: Yonik Seeley > Priority: Minor > > Copied From my Email: > Float.floatToRawIntBits (in Java1.4) gives the raw float bits without > normalization (like *(int*)&floatvar would in C). Since it doesn't do > normalization of NaN values, it's faster (and hopefully optimized to a > simple inline machine instruction by the JVM). > On my Pentium4, using floatToRawIntBits is over 5 times as fast as > floatToIntBits. > That can really add up in something like Similarity.floatToByte() for > encoding norms, especially if used as a way to compress an array of > float during query time as suggested by Doug. -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]