Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of python running acceleration

Shulou Source: shulou.com Published: 2022-06-01 02:48:53 09月21日 Update

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))

Tags: Methods code functions files loops runs content learning practice tutorials time conditions utf-8 testing utility deeper multiples interests compatibility words Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Technology Shulou Information MariaDB OPPO Reno