Python round() too slow, faster way to reduce precision?

2024/10/6 6:48:38

I am doing the following:

TOLERANCE = 13
some_float = ...
round(some_float, TOLERANCE)

This is run many times, so performance is important. I have to round some_float due to floating point representation error. I don't actually need to "round" the number in that sense, just remove digits after 13 trailing digits.

Is there a faster way to do this?

Answer

I've made some benches to compare round(some_float, TOLERANCE) with int(some_float * p + 0.5)/p (with p as 10**TOLERANCE) and here are the results:

  • round: 6.20 seconds
  • int divide+multiply: 3.53

my bench:

import timeTOLERANCE = 5
some_float = 12.2439924563634564564564
nb_loops = 10000000start_time = time.time()
for _ in range(nb_loops):r1 = round(some_float, TOLERANCE)
print(r1,time.time()-start_time)start_time = time.time()
p = float(10**TOLERANCE)
for _ in range(nb_loops):r2 = int(some_float * p + 0.5)/p
print(r2,time.time()-start_time)

result:

12.24399 6.208600997924805
12.24399 3.525486946105957

so the int solution is faster. round is probably better at handling the rounding of negative numbers (as someone commented, it makes a lot extra calls, so the code is more complex). The rounding may be different depending on the sign of the input number. Accuracy vs raw speed, again.

Add 0.5 or not to round or truncate. This seems to be a detail for you, but the int solution (provided that 10**TOLERANCE is precomputed) seems faster.

If you want to use that technique, you could be tempted to put the rounding code in a function:

TOLERANCE = 5
p = float(10**TOLERANCE)
def my_round_5(some_float):return int(some_float * p + 0.5)/p

and call it like this:

r2 = my_round(some_float)

that would be still faster than round, but a tad slower than using the formula inline (because function call isn't free)

note that I've used p = float(10**TOLERANCE) and not p = 10**TOLERANCE so the code is compatible with python 2 (else it would truncate the decimal part due to integer division)

https://en.xdnf.cn/q/70394.html

Related Q&A

Reading .doc file in Python using antiword in Windows (also .docx)

I tried reading a .doc file like - with open(file.doc, errors=ignore) as f:text = f.read()It did read that file but with huge junk, I cant remove that junk as I dont know from where it starts and where…

Error installing package with pip

Im trying to install a charting tool (matplotlib-v1.4.2) for python 3.4 in Windows 7, so far all my trails doesnt seem to do the job.Attempts:Ive downloaded pip from GitHub python -m pip install matplo…

Assign new values to certain tensor elements in Keras

I need to change the value of some elements of a tensor. I know what elements -- they are in a boolean tensor already.I dont see how to do this in keras code. But if I were using TensorFlow code I woul…

Making grid triangular mesh quickly with Numpy

Consider a regular matrix that represents nodes numbered as shown in the figure:I want to make a list with all the triangles represented in the figure. Which would result in the following 2 dimensional…

df [X].unique() and TypeError: unhashable type: numpy.ndarray

all,I have a column in a dataframe that looks like this:allHoldingsFund[BrokerMixed] Out[419]: 78 ML 81 CITI 92 ML 173 CITI 235 ML 262 ML 264 ML 25617 …

Python pandas idxmax for multiple indexes in a dataframe

I have a series that looks like this:delivery 2007-04-26 706 23 2007-04-27 705 10706 1089708 83710 13712 51802 4806 181…

No of Pairs of consecutive prime numbers having difference of 6 like (23,29) from 1 to 2 billion

How to find number of pairs of consecutive prime numbers having difference of 6 like (23,29) from 1 to 2 billion (using any programming language and without using any external libraries) with consideri…

Building a docker image for a flask app fails in pip

from alpine:latest RUN apk add --no-cache python3-dev \&& pip3 install --upgrade pipWORKDIR /backend COPY . /backendRUN pip --no-cache-dir install -r requirements.txt EXPOSE 5000 ENTRYPOINT [py…

Why is numba so fast?

I want to write a function which will take an index lefts of shape (N_ROWS,) I want to write a function which will create a matrix out = (N_ROWS, N_COLS) matrix such that out[i, j] = 1 if and only if j…

How to create a field with a list of foreign keys in SQLAlchemy?

I am trying to store a list of models within the field of another model. Here is a trivial example below, where I have an existing model, Actor, and I want to create a new model, Movie, with the field …