Skip to content

Memory saving / advice for "string". #4

Description

@dglaude

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. :-)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions