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 is the method of python running acceleration

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The main content of this article is to explain "what is the method of accelerating the operation of python". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the method of python running acceleration"?

I. Summary

1. Use pypy

2. Reduce functional calls

3. Reduce the opening of the file, that is, the call to with, put this call in front of the for loop, and then pass it to where you need it later

4. The judgment conditions of if function should be as many as possible.

Full acceleration (pypy)

2. Overall acceleration (pypy)

Replace python with pypy, and the compatibility of pypy does not affect the use of pure python code, because some pure python code often accelerates with pypy.

Test the code, for loop 10000000 times

Start = time.time () for i in range (10000000): print (I endend = ") end= time.time () print (f" takes {end-start} seconds > > ")

The time consumed by pypy is:

While python takes time to

Roughly three times, but the more cycles, the faster, it is said to be about six times.

Second, reduce the opening of files, that is, the call of with

The with of the original code is in the calling function, that is, every time the function is called, the file is opened and closed, resulting in a lot of time-consuming.

Def BMES (word,tag): with open (r "J:PyCharm Project Learning ongoing NLP tutorial NLP tutorial dataset part of speech tagging ature2ner.txt", "a +" Encoding= "utf-8") as fags: if len (word) = = 1: "single word" f_.write (word + "" + f "S-{tag.upper ()}" + ") else:" multiple words "for index" Word_ in enumerate (word): if index = = 0: f_.write (word_ + "" + f "B-{tag.upper ()}" + ") elif 0 < index < len (word)-1: f_.write (word_ +"+ f" M-{tag.upper ()} "+") Else: f_.write (word_ + "" + f "E-{tag.upper ()}" + "") # is subsequently called in multiple if-elif-else

The time spent is

The estimated time of tqdm is about 15 to 25 hours.

Put with in front of the loop

Such as

Pass in the contents of with as f _

The later time spent is:

The tests are as follows:

Import os, warnings,time,tqdmdef txt (word): with open ("ceshi.txt", "a +", encoding= "utf-8") as f: if len (str (word))

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