inbuilt function buffer()

Aldo Cortesi aldo at nullcube.com
Tue Jan 18 19:36:36 EST 2005


Thus spake km (km at mrna.tn.nic.in):

> 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
aldo at nullcube.com
http://www.nullcube.com
Off: (02) 9283 1131
Mob: 0419 492 863



More information about the Python-list mailing list