scxx_timings.py :  » Math » SciPy » scipy » scipy » weave » tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Math » SciPy 
SciPy » scipy » scipy » weave » tests » scxx_timings.py
import weave
import time

force = 0
N = 1000000

def list_append_scxx(a,Na):
    code = """
           for(int i = 0; i < Na;i++)
               a.append(i);
           """
    weave.inline(code,['a','Na'],force=force,verbose=2,compiler='gcc')

def list_append_c(a,Na):
    code = """
           for(int i = 0; i < Na;i++)
           {
               PyObject* oth = PyInt_FromLong(i);
               int res = PyList_Append(py_a,oth);
               Py_DECREF(oth);
               if(res == -1)
               {
                 PyErr_Clear();  //Python sets one
                 throw_error(PyExc_RuntimeError, "append failed");
               }
           }
           """
    weave.inline(code,['a','Na'],force=force,compiler='gcc')

def list_append_py(a,Na):
    for i in xrange(Na):
        a.append(i)

def time_list_append(Na):
    """ Compare the list append method from scxx to using the Python API
        directly.
    """
    print 'list appending times:',

    a = []
    t1 = time.time()
    list_append_c(a,Na)
    t2 = time.time()
    print 'py api: ', t2 - t1, '<note: first time takes longer -- repeat below>'

    a = []
    t1 = time.time()
    list_append_c(a,Na)
    t2 = time.time()
    print 'py api: ', t2 - t1

    a = []
    t1 = time.time()
    list_append_scxx(a,Na)
    t2 = time.time()
    print 'scxx:   ', t2 - t1

    a = []
    t1 = time.time()
    list_append_c(a,Na)
    t2 = time.time()
    print 'python: ', t2 - t1

#----------------------------------------------------------------------------
#
#----------------------------------------------------------------------------

def list_copy_scxx(a,b):
    code = """
           for(int i = 0; i < a.length();i++)
               b[i] = a[i];
           """
    weave.inline(code,['a','b'],force=force,verbose=2,compiler='gcc')

def list_copy_c(a,b):
    code = """
           for(int i = 0; i < a.length();i++)
           {
               int res = PySequence_SetItem(py_b,i,PyList_GET_ITEM(py_a,i));
               if(res == -1)
               {
                 PyErr_Clear();  //Python sets one
                 throw_error(PyExc_RuntimeError, "append failed");
               }
           }
           """
    weave.inline(code,['a','b'],force=force,compiler='gcc')

def list_copy_py(a,b):
    for item in a:
        b[i] = item

def time_list_copy(N):
    """ Compare the list append method from scxx to using the Python API
        directly.
    """
    print 'list copy times:',

    a = [0] * N
    b = [1] * N
    t1 = time.time()
    list_copy_c(a,b)
    t2 = time.time()
    print 'py api: ', t2 - t1, '<note: first time takes longer -- repeat below>'

    a = [0] * N
    b = [1] * N
    t1 = time.time()
    list_copy_c(a,b)
    t2 = time.time()
    print 'py api: ', t2 - t1

    a = [0] * N
    b = [1] * N
    t1 = time.time()
    list_copy_scxx(a,b)
    t2 = time.time()
    print 'scxx:   ', t2 - t1

    a = [0] * N
    b = [1] * N
    t1 = time.time()
    list_copy_c(a,b)
    t2 = time.time()
    print 'python: ', t2 - t1

if __name__ == "__main__":
    #time_list_append(N)
    time_list_copy(N)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.