Skip to content

Memory saving / advice for "string". #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dglaude opened this issue Apr 5, 2021 · 0 comments
Open

Memory saving / advice for "string". #4

dglaude opened this issue Apr 5, 2021 · 0 comments

Comments

@dglaude
Copy link

dglaude commented Apr 5, 2021

I am pretty sure there is something "special" about the way "string" are manadged in Python.

Something about "invariant" or I don't know what.

So when formatting string, even in a print there might be more or less optimal way to do it and avoid a lot of memory allocation (if it's in the stack, I guess it is fine, if it is in the heap, maybe this will make a lot of fragmentation over time).

I totally don't know the proper and best way of doing things... let's say for a program that just want to print, or display values.

There is the str() function that can convert a value into a string, and then you can concatenate.
There is the print("Value: ", value, ".") way, without formatting.

There is that notation that I have seen used:
line = "Temperature %0.1f C" % (bme680.temperature + temperature_offset)

I am pretty sure one is better than the other, no big saving, except if you do a lot of string processing...
But I would love to know the recommended way (I think that kattni already asked that kind of question in meeting and got an answer from danh or tannewt ... I wish I had a good enough memory to remember what is the best and why... and maybe the question was about what is more "pythonic" rather than what is more memory efficient).

So there could be things to say about "print" and "string" but this would need to be ask to more expert people. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant