inbuilt function buffer()

2005-01-18 Thread km
Hi all,
I which context is the inbuilt function buffer() used ? 
regards,
KM
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inbuilt function buffer()

2005-01-18 Thread Aldo Cortesi
Thus spake km ([EMAIL PROTECTED]):

 I which context is the inbuilt function buffer() used ? 

It's an efficiency measure. Say you have a string x. Taking
a slice of the string - x[a:a+10] - will implicitly create a
new string containing the specified data. Doing the same
using buffer - buffer(x, a, 10) - will not. Essentially,
buffer() gives you a window into a piece of contiguous data,
without the cost of making a copy of the data you want to
extract. 

To see how this might be useful, consider writing a simple
web server. The web server reads a local file into memory,
and then feeds it bit by bit to a socket until the data is
exhausted. Without using buffer, the server would have to
create a copy of each snippet of data fed to the socket.
Using buffer, however, the copies are avoided, and the data
can be consumed more efficently. The code below demonstrates
this idea in an elementary fashion  - on my system,
takebuf runs about twice as fast as take.


--

#!/usr/bin/env python2.3
import time

SIZE = 1024*1024*50
data = O*SIZE

def take(data):
for i in range(0, SIZE, 1024):
yield data[i:i+1024]

def takebuf(data):
for i in range(0, SIZE, 1024):
yield buffer(data, i, 1024)

def main():
start = time.time()
for i in take(data):
pass
print Plain:\t, time.time()-start

start = time.time()
for i in takebuf(data):
pass
print Buffer:\t, time.time()-start

if __name__ == __main__:
main()






Cheers,



Aldo



-- 
Aldo Cortesi
[EMAIL PROTECTED]
http://www.nullcube.com
Off: (02) 9283 1131
Mob: 0419 492 863
-- 
http://mail.python.org/mailman/listinfo/python-list