Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #78222
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed0.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.87.MISMATCH!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python@mrabarnett.plus.com> |
| 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; 'runtime': 0.09; 'python': 0.11; 'language,': 0.12; 'argument,': 0.16; 'caller.': 0.16; 'complexity,': 0.16; 'decisions,': 0.16; 'expecting': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'initially,': 0.16; 'inserting': 0.16; 'loops': 0.16; 'luckily,': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 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:In-Reply-To:1': 0.27; 'point': 0.28; 'fixed': 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; 'received:84': 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; '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; 'repeat.': 0.84; 'short,': 0.84; 'terrible': 0.84 |
| X-CM-Score | 0.00 |
| X-CNFS-Analysis | v=2.1 cv=MK7umapl c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=u9EReRu7m0cA:10 a=eM2qP8Knf1oA:10 a=4icewQsqUnYA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=3pfbhEuKyylxbrUe78EA:9 a=bcaZVYxok4sl5Ziu:21 a=QuaBBK--xYEH0JMH:21 a=QEXdDO2ut3YA:10 |
| X-AUTH | mrabarnett:2500 |
| Date | Tue, 23 Sep 2014 16:42:59 +0100 |
| From | MRAB <python@mrabarnett.plus.com> |
| User-Agent | Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Dynamically swapping between two algorithms |
| References | <54218831$0$29979$c3e8da3$5496439d@news.astraweb.com> |
| In-Reply-To | <54218831$0$29979$c3e8da3$5496439d@news.astraweb.com> |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.14270.1411487168.18130.python-list@python.org> (permalink) |
| Lines | 68 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1411487168 news.xs4all.nl 2944 [2001:888:2000:d::a6]:45795 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:78222 |
Show key headers only | View raw
On 2014-09-23 15:48, 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. > How about this: Try both of them initially, and use the faster one until it takes longer than the slower one did initially. Then try both of them again. Repeat.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Dynamically swapping between two algorithms Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-24 00:48 +1000 Re: Dynamically swapping between two algorithms Chris Angelico <rosuav@gmail.com> - 2014-09-24 01:24 +1000 Re: Dynamically swapping between two algorithms emile <emile@fenx.com> - 2014-09-23 08:33 -0700 Re: Dynamically swapping between two algorithms MRAB <python@mrabarnett.plus.com> - 2014-09-23 16:42 +0100 Re: Dynamically swapping between two algorithms Terry Reedy <tjreedy@udel.edu> - 2014-09-23 15:21 -0400 Re: Dynamically swapping between two algorithms C Smith <illusiontechniques@gmail.com> - 2014-09-23 13:54 -0700 Re: Dynamically swapping between two algorithms dieter <dieter@handshake.de> - 2014-09-24 09:44 +0200 Re: Dynamically swapping between two algorithms Cameron Simpson <cs@zip.com.au> - 2014-09-26 10:30 +1000 Re: Dynamically swapping between two algorithms Ethan Furman <ethan@stoneleaf.us> - 2014-09-25 20:11 -0700
csiph-web