Path: csiph.com!usenet.pasdenom.info!news.albasani.net!news.roellig-ltd.de!open-news-network.org!border2.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python.': 0.02; 'algorithm': 0.04; 'assign': 0.07; 'element': 0.07; 'elements.': 0.07; 'interpreter.': 0.07; 'level,': 0.07; 'linear': 0.09; 'optimizing': 0.09; 'way:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'language.': 0.14; 'algorithm.': 0.16; 'chris,': 0.16; 'cleaner': 0.16; 'constructs': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterators': 0.16; 'length,': 0.16; 'list.)': 0.16; 'loops': 0.16; 'simplest': 0.16; 'demonstrate': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; '(the': 0.22; 'code,': 0.22; 'cc:addr:python.org': 0.22; 'example.': 0.24; 'earlier': 0.24; 'cc:2**0': 0.24; 'first,': 0.26; 'task': 0.26; 'asking': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; "doesn't": 0.30; 'besides': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'fixing': 0.31; "they'll": 0.31; 'another': 0.32; 'subject:time': 0.33; 'could': 0.34; 'case,': 0.35; 'one,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'done': 0.36; 'similar': 0.36; 'turn': 0.37; 'list': 0.37; 'being': 0.38; 'thank': 0.38; 'question,': 0.38; 'list,': 0.38; 'rather': 0.38; 'sure': 0.39; 'how': 0.40; 'new': 0.61; 'effective': 0.61; 'simple': 0.61; "you're": 0.61; 'first': 0.61; "you'll": 0.62; "you've": 0.63; 'real': 0.63; 'more': 0.64; 'linked': 0.65; 'details': 0.65; 'worth': 0.66; 'benefit': 0.68; 'optimized': 0.68; 'consequences': 0.74; 'obvious': 0.74; '2015': 0.84; 'calculations': 0.84; 'complexity': 0.84; 'recursion,': 0.84; 'terrible': 0.84; 'to:none': 0.92; 'fight': 0.97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=K7jrbollV1fWh///yMlvc/7mV2dYso3RHgz5hQ/zSz0=; b=lze7bH9MJFCdhDDgg7fk3/tGecF9YmyL8HTyDFFEvigtE2833/RcCVBH4IzOm9yoLJ 1TYMAmjNIGVP0aMratRvsg70129FmSiCySn2fYWVf8s7b5BqZ0bZMgOOGLbOrANtKDEP 1VrIJfKqLC4gXEEn/iY+nF65M5xeqi9n2kSO9nE7gU25SEFNbiF0jKB3UUsQs1BGJpJs wT40kBVo8CH9GttuHszPOY86eW273QXkWLRt+OJoC1TVjwlyy0Pd3U0HhMETy8A6/mz2 jTMN7c3cz8RTdeiQpxIriLYkt8hihTDoGvWmQFC6Z6WT4N+xSYR4oqvn2ow6uxtx2l1T /TgQ== MIME-Version: 1.0 X-Received: by 10.43.39.1 with SMTP id tk1mr9144542icb.26.1431979447848; Mon, 18 May 2015 13:04:07 -0700 (PDT) In-Reply-To: References: <9ceklad15llnv3npejq9iuh91soci8aeqo@4ax.com> Date: Tue, 19 May 2015 06:04:07 +1000 Subject: Re: Slices time complexity From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431979450 news.xs4all.nl 2936 [2001:888:2000:d::a6]:43332 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90813 On Tue, May 19, 2015 at 5:49 AM, Mario Figueiredo wrote: > On Tue, 19 May 2015 05:36:44 +1000, Chris Angelico > wrote: > >>What's the point of optimizing slicing to allow you to use a poor >>algorithm, instead of fixing your algorithm? >> > > Chris, thank you for your input. But the code isn't really the > question, is it? > > It's just an example. It was being used earlier to demonstrate Time > Complexity calculations in another forum. It's not real live code. > Never will be. Besides we already have a min() function in Python. General rule of optimization: Do the simplest thing first, and make it more complicated only if the speed benefit is worth it. In this case, slicing a list is done in the obvious and simple way: construct a new list of the appropriate length, and assign all its elements. (The details may be optimized some at the C level, but it still constructs a new list.) You're asking why Python doesn't have a much more complicated system (Todd suggests views and COW; another way is to do a LISP-style linked list, which has similar consequences to views, but is more efficient if you do this specific thing of processing the first element and recursing for the rest), and the answer is: There's always a better way to write your algorithm. So if your code is intended to demonstrate how a poor algorithm can turn a linear task into a quadratic one, congrats! You've succeeded. If you're trying to showcase how terrible Python is, well, I'm sure you could do that in more effective ways, but they'll still come down to trying to write code using the Python interpreter. If you write idiomatic Python code, using iterators and loops rather than recursion, you'll find your code is cleaner and faster than it would be if you fight against the language. ChrisA