How to speed up a script by threading?

Liste des GroupesRevenir à cl tcl 
Sujet : How to speed up a script by threading?
De : mark (at) *nospam* qtrac.eu (Mark Summerfield)
Groupes : comp.lang.tcl
Date : 19. Jul 2024, 09:14:38
Autres entêtes
Message-ID : <nkmdnbODp8_zvQf7nZ2dnZfqn_udnZ2d@brightview.co.uk>
User-Agent : Pan/0.154 (Izium; 517acf4)
The program below (~95 LOC) is very slow.
One way to speed it up would be to process each directory given on the
command line in its own thread.
But I can't see how to do this.
Or is there a better way to make it faster?
I'm using Tcl/Tk 9.0b2 on Linux.

#!/usr/bin/env tclsh9

package require fileutil 1

namespace eval ef {}

proc ef::main {} {
    lassign [read_args] patterns dirs debug ;# may not return
    if {$debug} {
        puts "globs='$patterns'"
    }
    set filenames [list]
    foreach dir $dirs {
        if {$debug} {
            puts "folder='$dir'"
        }
        foreach filename [fileutil::findByPattern $dir $patterns] {
            lappend filenames $filename
        }
    }
    foreach filename [lsort $filenames] {
        puts $filename
    }
}

proc ef::read_args {} {
    set what ""
    set dirs [list]
    set debug false
    foreach arg $::argv {
        switch $arg {
            -h -
            --help {usage}
            -D -
            --debug {set debug true}
            default {
                if {$what eq ""} {
                    set what $arg
                } else {
                    lappend dirs $arg
                }
            }
        }
    }
    if {$what eq ""} {
        usage
    }
    set patterns [get_patterns $what]
    if {![llength $dirs]} {
        lappend dirs .
    }
    return [list $patterns $dirs $debug]
}

proc ef::usage {} {
    puts $::USAGE
    exit 1
}

proc ef::get_patterns what {
    if {[string index $what 0] ne "."} {
        return "*$what*"
    }
    set patterns [list *$what]
    switch $what {
        .c {lappend patterns *.h}
        .c++ {lappend patterns *.h *.hxx *.hpp *.h++ *.cxx *.cpp}
        .cpp {lappend patterns *.h *.hxx *.hpp *.h++ *.cxx *.c++}
        .tcl {lappend patterns *.tm}
        .py {lappend patterns *.pyw}
    }
    return $patterns
}

const USAGE {usage: efind.tcl [options] <what> [dir1 [dir2 …]]

options:
-h, --help      Show this usage message and quit.
-D, --debug     Show pattern and folders being processed.

Searches for files that match \"what\" in . or in any specified
folders, including recursively into subfolders.
\"what\" is either \".ext\" or text, e.g., .tcl or .py; or go.mod;
for .tcl searches *.{tcl,tm}, for .py *.{py,pyw}, for .c *.{c,h}
for .cpp or .c++ *.{h,hxx,hpp,h++,cxx,cpp,c++}; others as is.}

ef::main

Date Sujet#  Auteur
19 Jul 24 * How to speed up a script by threading?4Mark Summerfield
19 Jul 24 +* Re: How to speed up a script by threading?2Ralf Fassel
19 Jul 24 i`- Re: How to speed up a script by threading?1saito
19 Jul 24 `- Re: How to speed up a script by threading?1Harald Oehlmann

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal