Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the five python speed-up skills?

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What are the five python speed-up techniques? in view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

1. Skip the beginning of the iterative object string_from_file = "" / / Wooden:. / / LaoLi:. / Whole:. Wooden LaoLi... "" Import itertools for line in itertools.dropwhile (lambda line: line.startswith ("/ /"), string_from_file.split ("): print (line) 2, avoid data replication # not recommended Code time: 6.5 seconds def main (): size = 10000 for _ in range (size): value = range (size) value_list = [x for x in value] square_list = [x * x for x in value_list] main () # recommended Code time: 4.8s def main (): size = 10000 for _ in range (size): value = range (size) square_list = [x * x for x in value] # avoid meaningless copying 3, avoid variable intermediate variable # not recommended Code time: 0.07s def main (): size = 1000000 for _ in range (size): a = 3b = 5 temp = an a = b = temp main () # recommended writing Code time: 0.06s def main (): size = 1000000 for _ in range (size): a = 3b = 5a, b = b, a # without intermediate variable main () 4, loop optimization # is not recommended. Code time: 6.7 seconds def computeSum (size: int)-> int: sum_ = 0 I = 0 while I

< size: sum_ += i i += 1 return sum_ def main(): size = 10000 for _ in range(size): sum_ = computeSum(size) main()# 推荐写法。代码耗时:4.3秒def computeSum(size: int) ->

Int: sum_ = 0 for i in range (size): # for loop instead of while loop sum_ + = I return sum_ def main (): size = 10000 for _ in range (size): sum_ = computeSum (size) main ()

Implicit for loop replaces explicit for loop

# recommended writing method. Code time: 1.7s def computeSum (size: int)-> int: return sum (range (size)) # implicit for loop instead of explicit for loop def main (): size = 10000 for _ in range (size): sum = computeSum (size) main () 5, using numba.jit# recommended writing. Code time: 0.62 seconds # numba can compile the Python function JIT into machine code execution, greatly improving the speed of code execution. Import numba @ numba.jitdef computeSum (size: float)-> int: sum = 0 for i in range (size): sum + = i return sum def main (): size = 10000 for _ in range (size): sum = computeSum (size) main () the answers to the questions about the five python speed-up techniques are shared here. I hope the above content can be of some help to you, if you still have a lot of questions unsolved. You can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report