I have a buffer and I need to make sure I don't exceed certain size. If I do, I want to append the buffer to a file and empty it.
My code:
import sys
MAX_BUFFER_SIZE = 4 * (1024 ** 3)
class MyBuffer(object):
b = ""
def append(self, s):
if sys.getsizeof(self.b) > MAX_BUFFER_SIZE:
#...print to file... empty buffer
self.b = ""
else:
self.b += s
buffer = MyBuffer()
for s in some_text:
buffer.append(s)
However, this comparison (sys.getsizeof(self.buffer) > MAX_BUFFER_SIZE
) is way too slow (ie. without comparison, the whole execution takes less than 1 second, with comparison it takes like 5 minutes).
At the moment I can fit the whole some_string
to memory and so buffer actually never gets bigger than MAX_BUFFER_SIZE
, but I must make sure my code work for huge files (several TBs in size) too.
Edit:
This code runs in under 1 second:
import sys
buffer = ""
for s in some_text:
buffer += s
#print out to file
The problem is that the buffer might become too big.
Similarly, this code also runs under 1 second:
import sys
MAX_BUFFER_SIZE = 4 * (1024 ** 3)
class MyBuffer(object):
b = ""
def append(self, s):
print sys.getsizeof(self.b)
buffer = MyBuffer()
for s in some_text:
buffer.append(s)
EDIT 2:
Sorry, the slow part is actually appending to the buffer, not the comparison itself as I thought... When I was testing the code, I commented out the whole if / else
statement instead of just the first part.
Hence, is there an efficient way to keep a buffer?