Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!news.swapon.de!news.panservice.it!feed.xsnews.nl!border02.ams.xsnews.nl!feeder04.ams.xsnews.nl!feeder01.ams.xsnews.nl!abp002.ams.xsnews.nl!frontend-F10-19.ams.news.kpn.nl From: Cecil Westerhof Newsgroups: comp.lang.python Subject: Re: Function to show time to execute another function Organization: Decebal Computing References: <87k2vgowco.fsf@Equus.decebal.nl> X-Face: "(y8cC@tg_12{">GF'UXTW]FHI2wMiZNrnf'1EFQ&O#$m:f#O7+7}kR,v+Pti8=Vi/Z"g^?b"E X-Homepage: http://www.decebal.nl/ Date: Sun, 07 Jun 2015 10:22:17 +0200 Message-ID: <87fv64orly.fsf@Equus.decebal.nl> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:GlXZo2bCEPqsKafYEEfzz/HyZXM= MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Lines: 40 NNTP-Posting-Host: 81.207.62.244 X-Trace: 1433665792 news.kpn.nl 21101 81.207.62.244@kpn/81.207.62.244:56408 Xref: csiph.com comp.lang.python:92213 On Sunday 7 Jun 2015 09:39 CEST, Mark Lawrence wrote: > On 07/06/2015 07:39, Cecil Westerhof wrote: >> Sometimes I just want to know how much time a function takes, but >> at the same time I also want the result of the function. For this I >> wrote the following function: def time_test(function, *args): >> startTime = time.time() results = function(*args) endTime = >> time.time() print('It took {0} seconds'.format(endTime - >> startTime)) return results >> >> I can do: >> time_test(test_random, 100, 10 ** 5) >> This outputs: >> It took 17.01685857772827 seconds >> and returns: >> (98592, 100833, 0.977775133140936) >> >> When executing: >> time_test(test_random, 100, 10 ** 6) >> it outputs: >> It took 165.26371836662292 seconds >> and returns: >> (997103, 1002009, 0.9951038363926871) >> > > https://docs.python.org/3/library/timeit.html > https://docs.python.org/3/library/profile.html That only times the function. I explicitly mentioned I want both the needed time AND the output. Sadly the quality of the answers on this list is going down. Here I get an alternative that does only half what I want and when writing an alternative for ‘!find’ I am told I could use ‘!find’ (which only works in ipython, not python and which also not works with Windows). -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof