Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7029637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:33:54+00:00 2026-05-28T00:33:54+00:00

Below are two simple Cython methods I wrote. In g_cython() method I used additional

  • 0

Below are two simple Cython methods I wrote. In g_cython() method I used additional typing for numpy array a and b, but surprisingly g_cython() is twice slower than g_less_cython(). I wonder why is this happening? I thought adding that would make indexing on a and b much faster?

PS. I understand both functions can be vectorized in numpy — I am just exploring cython optimization tricks.

import numpy as np; 
cimport numpy as np;

def g_cython(np.ndarray[np.int_t, ndim = 1] a, percentile):
    cdef int i
    cdef int n = len(a)
    cdef np.ndarray[np.int_t, ndim = 1] b = np.zeros(n, dtype = 'int')
    for i in xrange(n):
        b[i] = np.searchsorted(percentile, a[i])
    return b


def g_less_cython(a, percentile):
    cdef int i
    b = np.zeros_like(a)
    for i in xrange(len(a)):
        b[i] = np.searchsorted(percentile, a[i])
    return b

my test case is when len(a) == 1000000 and len(percentile) = 100

def main3():
    n = 100000
    a = np.random.random_integers(0,10000000,n)
    per = np.linspace(0, 10000000, 101)

    q = time.time()
    b = g_cython(a, per)
    q = time.time() - q
    print q

q = time.time()
bb = g_less_cython(a, per)
q = time.time() - q
print q
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:33:55+00:00Added an answer on May 28, 2026 at 12:33 am

    I tested you code, g_cython is a slightly faster than g_less_cython.

    here is the test code

    import pyximport; pyximport.install()
    import search_sorted
    import numpy as np
    import time
    x = np.arange(100000, dtype=np.int32)
    y = np.random.randint(0, 100000, 100000)
    
    start = time.clock()
    search_sorted.g_cython(y, x)
    print time.clock() - start
    
    start = time.clock()
    search_sorted.g_less_cython(y, x)
    print time.clock() - start
    

    the output is:

    0.215430514708
    0.259622599945
    

    I turned off the boundscheck and wraparound flag:

    @cython.boundscheck(False)
    @cython.wraparound(False)
    def g_cython(np.ndarray[np.int_t, ndim = 1] a, percentile):
        ....
    

    The difference is not notable because the call of np.searchsorted(percentile, a[i]) is the critical part that used most of CPU.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple but subtle question. Below you see two different declaration variants
I have the simple function below which swap two characters of an array of
The code below plots some simple x-y data, but it has two problems that
I'm using Python 2.6.1 on Mac OS X. I have two simple Python files (below), but
Below program contains two show() functions in parent and child classes, but first show()
Of the two methods below, which do you prefer to read? Is there another
I have have a simple HTML form with say four input widgets (see below)...two
What I want to do is simple and below are details. I have two
I have two simple POCO classes; I'm trying to get the MyY property below
Thi simple code below outputs two alerts instead of one Google Chrome browser. Can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.