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 3998432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:31:29+00:00 2026-05-20T07:31:29+00:00

Few weeks ago I asked a question on increasing the speed of a function

  • 0

Few weeks ago I asked a question on increasing the speed of a function written in Python. At that time, TryPyPy brought to my attention the possibility of using Cython for doing so. He also kindly gave an example of how I could Cythonize that code snippet. I want to do the same with the code below to see how fast I can make it by declaring variable types. I have a couple of questions related to that. I have seen the Tutorial on the cython.org, but I still have some questions. They are closely related:

  1. I don’t know any C. What parts do I need to learn, to use Cython to declare variable types?
  2. What is the C type corresponding to python lists and tuples? For example, I can use double in Cython for float in Python. What do I do for lists? In general, where do I find the corresponding C type for a given Python type.

Any example of how I could Cythonize the code below would be really helpful. I have inserted comments in the code that give information about the variable type.

class Some_class(object):
    ** Other attributes and functions **
    def update_awareness_status(self, this_var, timePd):
        '''Inputs: this_var (type: float)
           timePd (type: int)
           Output: None'''

        max_number = len(self.possibilities)
        # self.possibilities is a list of tuples.
        # Each tuple is a pair of person objects. 

        k = int(math.ceil(0.3 * max_number))
        actual_number = random.choice(range(k))
        chosen_possibilities = random.sample(self.possibilities, 
                                         actual_number)
        if len(chosen_possibilities) > 0:
            # chosen_possibilities is a list of tuples, each tuple is a pair
            # of person objects. I have included the code for the Person class
            # below.
            for p1,p2 in chosen_possibilities:

                # awareness_status is a tuple (float, int)
                if p1.awareness_status[1] < p2.awareness_status[1]:                   
                    if p1.value > p2.awareness_status[0]:
                        p1.awareness_status = (this_var, timePd)
                    else:
                        p1.awareness_status = p2.awareness_status
                elif p1.awareness_status[1] > p2.awareness_status[1]:
                    if p2.value > p1.awareness_status[0]:
                        p2.awareness_status = (price, timePd)
                    else:
                        p2.awareness_status = p1.awareness_status
                else:
                    pass     

class Person(object):                                         
    def __init__(self,id, value):
        self.value = value
        self.id = id
        self.max_val = 50000
        ## Initial awareness status.          
        self.awarenessStatus = (self.max_val, -1)
  • 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-20T07:31:29+00:00Added an answer on May 20, 2026 at 7:31 am

    As a general note, you can see exactly what C code Cython generates for every source line by running the cython command with the -a “annotate” option. See the Cython documentation for examples. This is extremely helpful when trying to find bottlenecks in a function’s body.

    Also, there’s the concept of “early binding for speed” when Cython-ing your code. A Python object (like instances of your Person class below) use general Python code for attribute access, which is slow when in an inner loop. I suspect that if you change the Person class to a cdef class, then you will see some speedup. Also, you need to type the p1 and p2 objects in the inner loop.

    Since your code has lots of Python calls (random.sample for example), you likely won’t get huge speedups unless you find a way to put those lines into C, which takes a good amount of effort.

    You can type things as a tuple or a list, but it doesn’t often mean much of a speedup. Better to use C arrays when possible; something you’ll have to look up.

    I get a factor of 1.6 speedup with the trivial modifications below. Note that I had to change some things here and there to get it to compile.

    ctypedef int ITYPE_t
    
    cdef class CyPerson:
        # These attributes are placed in the extension type's C-struct, so C-level
        # access is _much_ faster.
        cdef ITYPE_t value, id, max_val
        cdef tuple awareness_status
    
        def __init__(self, ITYPE_t id, ITYPE_t value):
            # The __init__ function is much the same as before.
            self.value = value
            self.id = id
            self.max_val = 50000
            ## Initial awareness status.          
            self.awareness_status = (self.max_val, -1)
    
    NPERSONS = 10000
    
    import math
    import random
    
    class Some_class(object):
    
        def __init__(self):
            ri = lambda: random.randint(0, 10)
            self.possibilities = [(CyPerson(ri(), ri()), CyPerson(ri(), ri())) for i in range(NPERSONS)]
    
        def update_awareness_status(self, this_var, timePd):
            '''Inputs: this_var (type: float)
               timePd (type: int)
               Output: None'''
    
            cdef CyPerson p1, p2
            price = 10
    
            max_number = len(self.possibilities)
            # self.possibilities is a list of tuples.
            # Each tuple is a pair of person objects. 
    
            k = int(math.ceil(0.3 * max_number))
            actual_number = random.choice(range(k))
            chosen_possibilities = random.sample(self.possibilities,
                                             actual_number)
            if len(chosen_possibilities) > 0:
                # chosen_possibilities is a list of tuples, each tuple is a pair
                # of person objects. I have included the code for the Person class
                # below.
                for persons in chosen_possibilities:
                    p1, p2 = persons
                    # awareness_status is a tuple (float, int)
                    if p1.awareness_status[1] < p2.awareness_status[1]:
                        if p1.value > p2.awareness_status[0]:
                            p1.awareness_status = (this_var, timePd)
                        else:
                            p1.awareness_status = p2.awareness_status
                    elif p1.awareness_status[1] > p2.awareness_status[1]:
                        if p2.value > p1.awareness_status[0]:
                            p2.awareness_status = (price, timePd)
                        else:
                            p2.awareness_status = p1.awareness_status
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A few weeks ago I asked the question Is a PHP, Python, PostgreSQL design
A few weeks ago I asked a question about eliminating duplicate records in a
A few weeks ago, I asked a question about how to generate hierarchical XML
I asked from few weeks ago this question: How can I teach a beginner
A few weeks ago I asked about Application Servers. It happens that my bosses
I used a query a few weeks ago in MySQL that described a table
i asked this a few weeks ago, but couldnt get any of the suggested
I was actually asked this myself a few weeks ago, whereas I know exactly
I asked about this on the jquery forum a few weeks ago without luck,
A few weeks ago, I was assigned to evaluate all our programmers. I'm very

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.