On Jan 1, 2006, at 12:59 AM, Guillaume Proux wrote:
Ouch!
def to_bw(rawbuffer): retstr='' for e in rawbuffer : retstr = retstr + e*3 return retstr
This is a typical slow usage of string concat. the same loop would be much quicker execute like this
bwbuffer = "".join(map(lambda x:x*3,rawbuffer))
Guillaume
Guillaume,
I am a new python coder and found your one-line replacement for veto's function pretty neat. So neat I decided to run some benchmarks to see if it truly is faster.
From my test it appears veto's original function is faster. I tested that with my test_concat function. I then tested your way with test_map1 and a way I thought might be a bit faster with test_map2. As you can see from the results test_concat seems to have run the fastest.
However, my benchmarking code may be extremely flawed because, as I said, I am new to python. If you don't mind, could you correct my benchmarking code or explain why test_concat is running faster?
Results and code:
trevor@zeppelin ~ $ python bench.py concat: 5.902 map1: 9.917 map2: 9.801
trevor@zeppelin ~ $ cat bench.py import pygame
def to_bw(rawbuf): retstr='' for e in rawbuf: retstr = retstr + str(e*3) return retstr
def test_concat(test_count, test_list): t = pygame.time.get_ticks() for i in range(test_count): a = to_bw(test_list) return pygame.time.get_ticks() - t
def test_map1(test_count, test_list): t = pygame.time.get_ticks() for i in range(test_count): a = ''.join(map(lambda x: str(x*3), test_list)) return pygame.time.get_ticks() - t
def test_map2(test_count, test_list): t = pygame.time.get_ticks() f = lambda x: str(x*3) for i in range(test_count): a = ''.join(map(f, test_list)) return pygame.time.get_ticks() - t
def main(): test_list = [3, 9, 27] test_count = 10**6
pygame.init()
print 'concat: '+str(test_concat(test_count, test_list)/1000.0) print ' map1: '+str(test_map1(test_count, test_list)/1000.0) print ' map2: '+str(test_map2(test_count, test_list)/1000.0)
if __name__ == '__main__': main()
Thanks, __ Trevor Fancher http://fancher.org/
Attachment:
smime.p7s
Description: S/MIME cryptographic signature