Thank Tim, Raymond, and all.<br><br>In my test, Tim's t1 was fastest and Raymond's triple3 is very near to Tim's t1.<br>Anyway both of them took only 6~7% time of my original .extend() version. <br><br>I think that following n_ple() is a good generalized function that was 1~3% slower than t1() and triple3() for "triple".<br>
<br>*********<br>from itertools import *<br>def n_ple(inputlist, n):<br>    chain_from_iterable = chain.from_iterable; izip = izip<br>    return chain_from_iterable(izip(*[inputlist]*n))<br>*********<br><br> def t2(i):<br>

    r = range(3)<br>
    return (x for x in i for _ in r)<br> def t2_1(i):<br>

    r = range(3)<br>

    return [x for x in i for _ in r]<br><br>I didn't know that list(t2(inputlist)) is much faster than t2_1(inputlist), it's gonna be a helpful tip for me.<br><br>Thank you all.<br><br>Hyunchul<br><br><br><div class="gmail_quote">
On Tue, Nov 17, 2009 at 7:55 AM, Tim Chase <span dir="ltr"><<a href="mailto:python.list@tim.thechases.com">python.list@tim.thechases.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">Hyunchul Kim wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi, all.<br>
<br>
I want to improve speed of following simple function.<br>
Any suggestion?<br>
<br>
**********<br>
def triple(inputlist):<br>
  results = []<br>
  for x in inputlist:<br>
    results.extend([x,x,x])<br>
  return results<br>
**********<br>
</blockquote>
<br></div>
Several ways occur to me:<br>
<br>
  def t1(i):<br>
    for x in i:<br>
      yield x<br>
      yield x<br>
      yield x<br>
<br>
  def t2(i):<br>
    r = range(3)<br>
    return (x for x in i for _ in r)<br>
<br>
<br>
I prefer to return generators in case the input is large, but in both cases, you can just wrap it in list() like<br>
<br>
  list(t1(inputlist))<br>
<br>
or in t2(), you can just change it to use<br>
<br>
    return [x for x in i for _ in r]<br>
<br>
-tkc<br>
<br>
<br>
<br>
<br>
</blockquote></div><br>