Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.04; 'argument': 0.05; 'algorithms,': 0.07; 'dynamically': 0.07; 'subject:two': 0.07; 'advance': 0.07; '0.1': 0.09; 'calculating': 0.09; 'caller': 0.09; 'performs': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'runtime': 0.09; 'python': 0.11; 'language,': 0.12; 'argument,': 0.16; 'caller.': 0.16; 'complexity,': 0.16; 'decisions,': 0.16; 'expecting': 0.16; 'inserting': 0.16; 'loops': 0.16; 'luckily,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'spawn': 0.16; 'subject:between': 0.16; 'thoughts?': 0.16; 'threads,': 0.16; 'true:': 0.16; 'wrote:': 0.18; 'starts': 0.20; 'import': 0.22; 'header:User-Agent:1': 0.23; 'large,': 0.24; 'source': 0.25; 'first,': 0.26; 'this:': 0.26; 'second': 0.26; 'certain': 0.27; 'values': 0.27; 'gets': 0.27; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'fixed': 0.29; 'am,': 0.29; 'nature': 0.30; "i'm": 0.30; 'code': 0.31; "d'aprano": 0.31; 'large.': 0.31; 'option.': 0.31; 'steven': 0.31; 'trivial': 0.31; 'values.': 0.31; 'quite': 0.32; "i'd": 0.34; 'could': 0.34; "can't": 0.35; 'problem.': 0.35; '(2)': 0.35; 'test': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'should': 0.36; 'seconds': 0.37; 'two': 0.37; 'performance': 0.37; '(3)': 0.38; 'to:addr:python-list': 0.38; 'short': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'tell': 0.60; 'hope': 0.61; 'break': 0.61; 'entire': 0.61; 'range': 0.61; 'first': 0.61; 'reach': 0.63; 'different': 0.65; 'relatively': 0.65; 'talking': 0.65; 'hours': 0.66; 'due': 0.66; 'minutes': 0.67; 'between': 0.67; 'determine': 0.67; '(5)': 0.74; 'million': 0.74; 'other.': 0.75; 'algorithm,': 0.84; 'catastrophic': 0.84; 'completes': 0.84; 'harness': 0.84; 'received:pacbell.net': 0.84; 'short,': 0.84; 'terrible': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: emile Subject: Re: Dynamically swapping between two algorithms Date: Tue, 23 Sep 2014 08:33:02 -0700 References: <54218831$0$29979$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-69-226-129-65.dsl.pltn13.pacbell.net User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: <54218831$0$29979$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 73 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1411486443 news.xs4all.nl 2865 [2001:888:2000:d::a6]:42375 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78221 Add a timing harness and use a test interval (N) and call LARGE every Nth loop until LARGE's timing is better than the prior SHORT's run. Emile On 09/23/2014 07:48 AM, Steven D'Aprano wrote: > I have a certain calculation which can be performed two radically different > ways. With the first algorithm, let's call it SHORT, performance is very > fast for small values of the argument, but terrible for large values. For > the second algorithm, LARGE, performance is quite poor for small values, > but excellent for large values. > > To add to the complexity, I perform this calculation repeatedly, in a loop: > > value = 1 > while True: > x = SHORT(value) # Or should I use LARGE? Decisions, decisions... > process(x) > value += some_increment() > if condition: break > > Luckily, the value never gets smaller. So if I could somehow determine a > cut-over point, CUTOFF, I might write my loop like this: > > value = 1 > while True: > f = SHORT if value < CUTOFF else LARGE > x = f(value) > process(x) > value += some_increment() > if condition: break > > alas, the CUTOVER point is likely to be machine-dependent. Take it as a > given that inserting a fixed CUTOVER point into the source code (say, > ``CUTOVER = 123456``) is not likely to be very effective, and dynamically > calculating it at import time is impractical. > > *If* Python was a different language, I would spawn two threads, one using > SHORT and the other using LARGE, then which ever completes first, I'd just > kill the other. Alas, this won't work because (1) the GIL and (2) you > cannot forcibly kill threads, only ask them to die and hope they listen. > > I am seeking other ideas for dynamically swapping between the two > algorithms, based on runtime information. Any thoughts? > > > (1) I can't tell in advance how many loops I will make. > > (2) Both SHORT and LARGE get slower as the size of their argument increases. > This is unavoidable due to the nature of the problem. > > (3) SHORT starts off relatively speedy, significantly faster than LARGE for > the first few tens of thousands of loops. I'm not talking about trivial > micro-optimizations here, I'm talking about the difference between 0.1 > second for SHORT versus 10 seconds for LARGE. > > (4) But as the size of the argument increases, SHORT suffers catastrophic > quadratic slowdown, while LARGE slows down only linearly. (Give or take.) > > (5) Consequently, by the time I reach a few million loops, the difference is > now between (say) 5 minutes for LARGE versus 15 hours for SHORT. > > (6) There is no single algorithm which performs acceptably across the entire > range of values I'm expecting to process in practice. > > (7) Leaving the choice up to the caller is not an option. I am the caller. > > > >