Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Cython array creation is horribly slow

Received: by 10.224.205.65 with SMTP id fp1mr7173764qab.4.1352593708067;
        Sat, 10 Nov 2012 16:28:28 -0800 (PST)
X-BeenThere: cython-users@googlegroups.com
Received: by 10.229.171.224 with SMTP id i32ls8346895qcz.8.gmail; Sat, 10 Nov
 2012 16:28:21 -0800 (PST)
Received: by 10.224.205.65 with SMTP id fp1mr7173744qab.4.1352593701712;
        Sat, 10 Nov 2012 16:28:21 -0800 (PST)
Received: by 10.224.205.65 with SMTP id fp1mr7173743qab.4.1352593701702;
        Sat, 10 Nov 2012 16:28:21 -0800 (PST)
Return-Path: <markflorisso...@gmail.com>
Received: from mail-qa0-f50.google.com (mail-qa0-f50.google.com [209.85.216.50])
        by gmr-mx.google.com with ESMTPS id f17si118654qck.1.2012.11.10.16.28.21
        (version=TLSv1/SSLv3 cipher=OTHER);
        Sat, 10 Nov 2012 16:28:21 -0800 (PST)
Received-SPF: pass (google.com: domain of markflorisso...@gmail.com designates 209.85.216.50 as permitted sender) client-ip=209.85.216.50;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of markflorisso...@gmail.com designates 209.85.216.50 as permitted sender) smtp.mail=markflorisso...@gmail.com; dkim=pass header...@gmail.com
Received: by mail-qa0-f50.google.com with SMTP id k1so1171650qaf.2
        for <cython-users@googlegroups.com>; Sat, 10 Nov 2012 16:28:21 -0800 (PST)
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:to
         :content-type;
        bh=X+4Mh7YlOByNHmEGt+TYwPLLPrK4ot57TOKfz4XS6KU=;
        b=AHQ++gyEYYcE6EwXZ7BA2HPZIjwf99M1652Rdr7Xl1kzrKVmxuo5dHITEcBy51Jcg3
         X94rqjJtXXiLTE3/1zvrfbtUaVOD4Fj+4vsLXX6sHr2KgBmH1+sI6c9lrev3Jh5OcDQU
         rDJfh1u1oTDj3U8LIlBq4bMctri+USEmcYd83YX1JAovpHkvX40SYflHtg1BruTEU6y3
         fhvcsbJzUF3P01BpOp2DLy6jy7+cT++bWMLNjBhEWhRQoX8Xn7Op1t4ER7LcmoAkRNlF
         Nscda39kxB+2c/nGOHhr4/BRFZCWDcjCGlVUZO7bI9VE1+/VAkPnIEgL5WEHV782oOzo
         s1vQ==
MIME-Version: 1.0
Received: by 10.49.104.3 with SMTP id ga3mr23525035qeb.11.1352593701483; Sat,
 10 Nov 2012 16:28:21 -0800 (PST)
Received: by 10.49.64.4 with HTTP; Sat, 10 Nov 2012 16:28:21 -0800 (PST)
In-Reply-To: <CAJQnXJdYR_s9FCg2eMPmSJjG=XY2E7kand06whA2_BQycuA...@mail.gmail.com>
References: <CAJQnXJdYR_s9FCg2eMPmSJjG=XY2E7kand06whA2_BQycuA...@mail.gmail.com>
Date: Sun, 11 Nov 2012 00:28:21 +0000
Message-ID: <CANg26EU2cukL91ght37x+bq8TSVrYjB6P9T3Vt7sbt6b20x...@mail.gmail.com>
Subject: Re: [cython-users] Cython array creation is horribly slow
From: mark florisson <markflorisso...@gmail.com>
To: cython-users@googlegroups.com
Content-Type: text/plain; charset=UTF-8

On 10 November 2012 18:21, Ian Bell <ian.h.b...@gmail.com> wrote:
> I have been playing with memory views and cython arrays.  I am not sure what
> exactly is going wrong, but it seems like there is something not right about
> how cython allocates memory for cython arrays (as in
> http://docs.cython.org/src/userguide/memoryviews.html).  Creating of memory
> views are also horribly slow.  Or I have done something braindead in my code
> but I don't think so.  The docs for cython arrays are pretty much
> non-existent.  Where are they in the source tree?  I couldn't find the
> cython.view module anywhere

cython.view is a fake module. The cython array implementation and
other things can be found in Cython/Utility/MemoryView.pyx.

The reason it is slow is because no one ever cared to optimize it. It
allocates shape and strides independently on the heap. So it contains
at least 3 malloc calls, and a bunch of other code.

If you want to make it really fast, implement it with a free list,
only malloc the data and make sure the code in the constructor is
native where possible. No one is actively working on it, but it's just
Cython code, so patches would be appreciated and merged.

> I ran a quick check to see what is going on with the array creation in
> cython.  I have attached the pyx and the setup file used to generate this
> data and copied it at the end of the email too.  FWIW, I'm on Windows 7
> Python 2.7, visual studio 2008 wih a fresh cython from GIT head.  The array
> creation time for cython arrays seems to be constant independent of length
> of the array? To be fair, the numpy example isn't exactly the same, but
> still, 10 times slower in cython than pure c is not too good.  2us per array
> creation for a 10 element array is very slow, a speed that is just not
> cutting it for my severely speed constrained application.  In fact what I
> was trying to was to extend the code that was started in
> https://groups.google.com/forum/?fromgroups=#!topic/cython-users/PpIokkZVOrA
> to use the native cython arrays in order to avoid the hacking in the array
> example provided.
>
> My idea is I am going to wrap my own 1-d array floating point datatype
> extension type from the ground up with a focus on speed for the element-wise
> operations.  I'll still include the same interface by implementing most or
> all of the special methods.
>
> Incidentally, it does not seem to be possible to inherit from array.array
> class in 0.17.1
>
> Here is the summary (averaged elapsed time per call for one million calls):
> Elapsed time to make array with length of 1 is 2.40862075722 us
> Elapsed time to make array with length of 10 is 2.36220109038 us
> Elapsed time to make array with length of 100 is 2.37456325685 us
> Elapsed time to make array with length of 1000 is 2.37144104029 us
> Elapsed time to make array with length of 10000 is 2.72447256951 us
> Elapsed time to make numpy array with length of 1 is 0.806911490145 us
> Elapsed time to make numpy array with length of 10 is 0.869661726899 us
> Elapsed time to make numpy array with length of 100 is 0.926880930014 us
> Elapsed time to make numpy array with length of 1000 is 1.52742669012 us
> Elapsed time to make numpy array with length of 10000 is 7.3662581456 us
> Elapsed time with raw c-allocation with length of 1 is 0.195063960543 us
> Elapsed time with raw c-allocation with length of 10 is 0.196661594872 us
> Elapsed time with raw c-allocation with length of 100 is 0.204236565547 us
> Elapsed time with raw c-allocation with length of 1000 is 0.211111834023 us
> Elapsed time with raw c-allocation with length of 10000 is 0.363121851293 us
>
> mv_test.pyx:
> ------------------------------------------------
> from cython.view cimport array as cvarray
> import time
>
> import numpy as np
> cimport numpy as np
>
> from libc.stdlib cimport malloc, free
>
> cdef long N = 1000000
>
> cdef double* ptr
>
> for L in [1,10,100,1000,10000]:
>     t1 = time.clock()
>     for i in range(N):
>         a = cvarray((L,),sizeof(double),'d')
>     t2 = time.clock()
>     print 'Elapsed time to make array with length of ' +str(L)+' is
> '+str((t2-t1)/N*1e6)+' us'
>
> for L in [1,10,100,1000,10000]:
>     t1 = time.clock()
>     for i in range(N):
>         a = np.arange(L)
>     t2 = time.clock()
>     print 'Elapsed time to make numpy array with length of ' +str(L)+' is
> '+str((t2-t1)/N*1e6)+' us'
>
> for L in [1,10,100,1000,10000]:
>     t1 = time.clock()
>     for i in range(N):
>         ptr = <double*> malloc(sizeof(double) * L)
>         free(ptr)
>     t2 = time.clock()
>     print 'Elapsed time with raw c-allocation with length of ' +str(L)+' is
> '+str((t2-t1)/N*1e6)+' us'
>
>
> setup.py
> ---------------------------
> from distutils.core import setup
> from Cython.Build import cythonize
> import numpy
>
> import Cython
>
> #This will generate HTML to show where there are still pythonic bits hiding
> out
> Cython.Compiler.Options.annotate = True
>
> setup(
>     name = "My hello app",
>     ext_modules = cythonize('mv_test.pyx'), # accepts a glob pattern
>     include_dirs = [numpy.get_include()]
> )