Reputation: 1
I am not a programmer but am facing a programming problem. I need to reduce the memory consumption of a Python code. One option is to reduce the variable precision. In this regard:
Kindly help me. These questions may be stupid, but I have very less knowledge of Programming.
Thanks...
Upvotes: 0
Views: 252
Reputation: 7177
Pure CPython (sans numpy, etc.) floats are implemented as C doubles. http://www.ibm.com/developerworks/opensource/library/os-python1/
Yes, the types are associated with values, not variables. You can check for something being a Python float with isinstance(something, float).
You could perhaps try objgraph to see what's using your memory. http://mg.pov.lt/objgraph/
There's also a possibility that you are leaking memory, or merely need a garbage collection. And it could be that your machine just doesn't have much memory - sometimes it's cheaper to throw a little extra RAM, or even a little more swap space, at a constrained memory problem.
It's possible that using a low precision, alternative numeric representation would help - perhaps http://pypi.python.org/pypi/Simple%20Python%20Fixed-Point%20Module/0.6 ?.
Upvotes: 0
Reputation: 3638
If you are already using numpy
you can set the dtype
field to the type that you want (documentation). You'll get a little more flexibility there for typing, but in general you aren't going to get a lot of control over variable precision in Python. You might also have some luck if you want to go through the structural overhead of using the static types from Cython, though if you are new to programming that may not be the best route.
Beyond that, you haven't given us a lot to work with regarding your actual problem and why you feel that the variable precision is the best spot for optimization.
Upvotes: 1