[Tutor] Loop optimization

wormwood_3 wormwood_3 at yahoo.com
Tue Aug 21 02:21:52 CEST 2007


I ran a few tests, with the following results:

1. Timing using the time module:
       * Using for loop, src code:
                    import time
                    start = time.time()
                    for word in self.dictcontents:
                        self.potdomains.append(word + suffix1)
                        self.potdomains.append(word + suffix2)
                    end = time.time()
                    runtime =  end - start
                    print "Using time(), for loop took %s s" % runtime

        ** I obtained the following results (using the full agid-4 dictionary, ~112K entries):
        python domainspotter.py --file resources/agid-4/infl.txt 
        Using time(), for loop took 0.132480859756 s
        python domainspotter.py --file resources/agid-4/infl.txt 
        Using time(), for loop took 0.143032073975 s
        python domainspotter.py --file resources/agid-4/infl.txt 
        Using time(), for loop took 0.135424137115 s

       * Using generator, src code:
            def suffixGen(self, words):
                suffix1 = ".com"
                suffix2 = ".net"
                for word in words:
                    yield word + suffix1
                    yield word + suffix2
            def domainify(self):
                self.potdomains = []
                words = self.dictcontents
                import time
                start = time.time()
                self.potdomains = list(CheckDomains.suffixGen(self, words))
                end = time.time()
                runtime =  end - start
                print "Using time(), generator took %s s" % runtime

        ** I obtained the following results (using the full agid-4 dictionary, ~112K entries):
        python domainspotter.py --file resources/agid-4/infl.txt 
        Using time(), generator took 0.0830721855164 s
        python domainspotter.py --file resources/agid-4/infl.txt 
        Using time(), generator took 0.0818212032318 s
        python domainspotter.py --file resources/agid-4/infl.txt 
        Using time(), generator took 0.0830278396606 s


This revealed that the generator seemed to be much faster, around 60% faster. 

I then wanted to try both possibilities with the timeit module, but was unable to get it working. I will start a new thread on that next, however, in case anyone has any further thoughts on the for loop versus generator issue.

-Sam




More information about the Tutor mailing list