Python Forum
Why the multithread does not reduce the execution time?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why the multithread does not reduce the execution time?
#2
for the main program, try running:
import cprofile
import os, time, shutil, tarfile #fnmatch
from PIL import Image
import shutil
 
def main():
    all_files=[]
    sext = []
    temp = ""
    print(time.strftime("%H|%M|%S"))
    
    #chiedo all'utente che estensioni vuole cercare, in questo caso a me interessano i jpg, quindi inserisco: .jpg 
    #dopo aver scelto le estensioni, scrivo 0 e invio
    while True:
        sext.append(input("cerca: "))
        if sext[-1] == "0":
            del sext[-1]
            break
    sext = tuple(sext)
    
    #naviga fra le cartelle di Files recuperati, e cerca tutti i file che finiscono con "sext", รจ una tupla di estensioni, in questo caso, mi interessa solo ".jpg"
    for parent, directories, filenames in os.walk("/media/mionomeutente/PENNA USB/Files recuperati"):
        for x in filenames:
            if x.endswith(sext):
                fileDaAnalizzare = parent+'/'+x
    
                #apre il file con estensione specificata, e verifica che l'immagine abbia una certa grandezza
                try:
                    im = Image.open(fileDaAnalizzare)
                    width, height = im.size
                    if(width > 350 and height >350):
                        document_path = os.path.join(parent,x)
                        print(document_path)
    
                        #copio semplicemente l'immagine che rispetta le mie esigenze nella cartella grandi
                        shutil.copy(document_path, '/media/mionomeutente/PENNA USB/grandi')
                    
                except:
                    pass
                
    
    print(time.strftime("%H|%M|%S"))


if __name__ == '__main__':
    cProfile.run('main()')
And see if you can identify the bottleneck.
then try same with second script
Reply


Messages In This Thread
RE: Why the multithread does not reduce the execution time? - by Larz60+ - Nov-22-2019, 09:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I multithread to optimize a groupby task: davisc4468 0 760 Jun-30-2023, 02:45 PM
Last Post: davisc4468
  reduce nested for-loops Phaze90 11 2,022 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  Adding values with reduce() function from the list of tuples kinimod 10 2,814 Jan-24-2023, 08:22 AM
Last Post: perfringo
  How to measure execution time of a multithread loop spacedog 2 2,935 Apr-24-2021, 07:52 AM
Last Post: spacedog
  How do I reduce the time to Invoke Macro via Python? JaneTan 1 2,186 Dec-28-2020, 06:46 AM
Last Post: buran
  PyCharm Script Execution Time? muzikman 3 8,591 Dec-14-2020, 11:22 PM
Last Post: muzikman
  How to reduce the following code to run in sequence? Giggel 4 2,734 Jun-28-2020, 01:31 AM
Last Post: Giggel
  Help to reduce time to execute the code prakash52kar 1 2,267 Oct-14-2019, 10:56 AM
Last Post: scidam
  Time execution of a "for loop" with zip different in 2 equivalent context sebastien 1 1,957 Oct-11-2019, 11:07 AM
Last Post: sebastien
  How to get memory usage and execution time of each line in python SriRajesh 2 3,211 Mar-07-2019, 12:59 PM
Last Post: SriRajesh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020