Dive Into Python-Chapter 18. Performance Tuning
Số trang: 46
Loại file: pdf
Dung lượng: 0.00 B
Lượt xem: 9
Lượt tải: 0
Xem trước 5 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu dive into python-chapter 18. performance tuning, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Dive Into Python-Chapter 18. Performance Tuning Chapter 18. Performance TuningPerformance tuning is a many-splendored thing. Just because Python is aninterpreted language doesnt mean you shouldnt worry about codeoptimization. But dont worry about it too much.18.1. Diving inThere are so many pitfalls involved in optimizing your code, its hard toknow where to start.Lets start here: are you sure you need to do it at all? Is your code really sobad? Is it worth the time to tune it? Over the lifetime of your application,how much time is going to be spent running that code, compared to the timespent waiting for a remote database server, or waiting for user input?Second, are you sure youre done coding? Premature optimization is likespreading frosting on a half-baked cake. You spend hours or days (or more)optimizing your code for performance, only to discover it doesnt do whatyou need it to do. Thats time down the drain.This is not to say that code optimization is worthless, but you need to look atthe whole system and decide whether its the best use of your time. Everyminute you spend optimizing code is a minute youre not spending addingnew features, or writing documentation, or playing with your kids, or writingunit tests.Oh yes, unit tests. It should go without saying that you need a complete setof unit tests before you begin performance tuning. The last thing you need isto introduce new bugs while fiddling with your algorithms.With these caveats in place, lets look at some techniques for optimizingPython code. The code in question is an implementation of the Soundexalgorithm. Soundex was a method used in the early 20th century forcategorizing surnames in the United States census. It grouped similar-sounding names together, so even if a name was misspelled, researchers hada chance of finding it. Soundex is still used today for much the same reason,although of course we use computerized database servers now. Mostdatabase servers include a Soundex function.There are several subtle variations of the Soundex algorithm. This is the oneused in this chapter: 1. Keep the first letter of the name as-is. 2. Convert the remaining letters to digits, according to a specific table: * B, F, P, and V become 1. * C, G, J, K, Q, S, X, and Z become 2. * D and T become 3. * L becomes 4. * M and N become 5. * R becomes 6. * All other letters become 9. 3. Remove consecutive duplicates. 4. Remove all 9s altogether. 5. If the result is shorter than four characters (the first letter plus threedigits), pad the result with trailing zeros. 6. if the result is longer than four characters, discard everything after thefourth character.For example, my name, Pilgrim, becomes P942695. That has no consecutiveduplicates, so nothing to do there. Then you remove the 9s, leaving P4265.Thats too long, so you discard the excess character, leaving P426.Another example: Woo becomes W99, which becomes W9, which becomesW, which gets padded with zeros to become W000.Heres a first attempt at a Soundex function:Example 18.1. soundex/stage1/soundex1a.pyIf you have not already done so, you can download this and other examplesused in this book.import string, recharToSoundex = {A: 9, B: 1,C: 2,D: 3,E: 9,F: 1,G: 2,H: 9,I: 9,J: 2,K: 2,L: 4,M: 5,N: 5,O: 9,P: 1,Q: 2,R: 6,S: 2, T: 3, U: 9, V: 1, W: 9, X: 2, Y: 9, Z: 2}def soundex(source): convert string to Soundex equivalent # Soundex requirements: # source string must be at least 1 character # and must consist entirely of letters allChars = string.uppercase + string.lowercase if not re.search(^[%s]+$ % allChars, source): return 0000# Soundex algorithm:# 1. make first character uppercasesource = source[0].upper() + source[1:]# 2. translate all other characters to Soundex digitsdigits = source[0]for s in source[1:]: s = s.upper() digits += charToSoundex[s]# 3. remove consecutive duplicatesdigits2 = digits[0]for d in digits[1:]: if digits2[-1] != d: digits2 += d # 4. remove all 9s digits3 = re.sub(9, , digits2) # 5. pad end with 0s to 4 characters while len(digits3) < 4: digits3 += 0 # 6. return first 4 characters return digits3[:4]if __name__ == __main__: from timeit import Timer names = (Woo, Pilgrim, Flingjingwaller) for name in names: statement = soundex(%s) % name t = Timer(statement, from __main__ import soundex) print name.ljust(15), soundex(name), min(t.repeat())Further Reading on Soundex * Soundexing and Genealogy gives a chronology of the evolution of theSoundex and its regional variations.18.2. Using the timeit Mo ...
Nội dung trích xuất từ tài liệu:
Dive Into Python-Chapter 18. Performance Tuning Chapter 18. Performance TuningPerformance tuning is a many-splendored thing. Just because Python is aninterpreted language doesnt mean you shouldnt worry about codeoptimization. But dont worry about it too much.18.1. Diving inThere are so many pitfalls involved in optimizing your code, its hard toknow where to start.Lets start here: are you sure you need to do it at all? Is your code really sobad? Is it worth the time to tune it? Over the lifetime of your application,how much time is going to be spent running that code, compared to the timespent waiting for a remote database server, or waiting for user input?Second, are you sure youre done coding? Premature optimization is likespreading frosting on a half-baked cake. You spend hours or days (or more)optimizing your code for performance, only to discover it doesnt do whatyou need it to do. Thats time down the drain.This is not to say that code optimization is worthless, but you need to look atthe whole system and decide whether its the best use of your time. Everyminute you spend optimizing code is a minute youre not spending addingnew features, or writing documentation, or playing with your kids, or writingunit tests.Oh yes, unit tests. It should go without saying that you need a complete setof unit tests before you begin performance tuning. The last thing you need isto introduce new bugs while fiddling with your algorithms.With these caveats in place, lets look at some techniques for optimizingPython code. The code in question is an implementation of the Soundexalgorithm. Soundex was a method used in the early 20th century forcategorizing surnames in the United States census. It grouped similar-sounding names together, so even if a name was misspelled, researchers hada chance of finding it. Soundex is still used today for much the same reason,although of course we use computerized database servers now. Mostdatabase servers include a Soundex function.There are several subtle variations of the Soundex algorithm. This is the oneused in this chapter: 1. Keep the first letter of the name as-is. 2. Convert the remaining letters to digits, according to a specific table: * B, F, P, and V become 1. * C, G, J, K, Q, S, X, and Z become 2. * D and T become 3. * L becomes 4. * M and N become 5. * R becomes 6. * All other letters become 9. 3. Remove consecutive duplicates. 4. Remove all 9s altogether. 5. If the result is shorter than four characters (the first letter plus threedigits), pad the result with trailing zeros. 6. if the result is longer than four characters, discard everything after thefourth character.For example, my name, Pilgrim, becomes P942695. That has no consecutiveduplicates, so nothing to do there. Then you remove the 9s, leaving P4265.Thats too long, so you discard the excess character, leaving P426.Another example: Woo becomes W99, which becomes W9, which becomesW, which gets padded with zeros to become W000.Heres a first attempt at a Soundex function:Example 18.1. soundex/stage1/soundex1a.pyIf you have not already done so, you can download this and other examplesused in this book.import string, recharToSoundex = {A: 9, B: 1,C: 2,D: 3,E: 9,F: 1,G: 2,H: 9,I: 9,J: 2,K: 2,L: 4,M: 5,N: 5,O: 9,P: 1,Q: 2,R: 6,S: 2, T: 3, U: 9, V: 1, W: 9, X: 2, Y: 9, Z: 2}def soundex(source): convert string to Soundex equivalent # Soundex requirements: # source string must be at least 1 character # and must consist entirely of letters allChars = string.uppercase + string.lowercase if not re.search(^[%s]+$ % allChars, source): return 0000# Soundex algorithm:# 1. make first character uppercasesource = source[0].upper() + source[1:]# 2. translate all other characters to Soundex digitsdigits = source[0]for s in source[1:]: s = s.upper() digits += charToSoundex[s]# 3. remove consecutive duplicatesdigits2 = digits[0]for d in digits[1:]: if digits2[-1] != d: digits2 += d # 4. remove all 9s digits3 = re.sub(9, , digits2) # 5. pad end with 0s to 4 characters while len(digits3) < 4: digits3 += 0 # 6. return first 4 characters return digits3[:4]if __name__ == __main__: from timeit import Timer names = (Woo, Pilgrim, Flingjingwaller) for name in names: statement = soundex(%s) % name t = Timer(statement, from __main__ import soundex) print name.ljust(15), soundex(name), min(t.repeat())Further Reading on Soundex * Soundexing and Genealogy gives a chronology of the evolution of theSoundex and its regional variations.18.2. Using the timeit Mo ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính công nghệ thông tin quản trị mạng tin học computer networkTài liệu liên quan:
-
52 trang 434 1 0
-
24 trang 359 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 321 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 311 0 0 -
74 trang 304 0 0
-
96 trang 299 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 291 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 286 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 270 0 0