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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:16:36+00:00 2026-06-16T04:16:36+00:00

I have a function which just basically makes lots of calls to a simple

  • 0

I have a function which just basically makes lots of calls to a simple defined hash function and tests to see when it finds a duplicate. I need to do lots of simulations with it so would like it to be as fast as possible. I am attempting to use cython to do this. The cython code is currently called with a normal python list of integers with values in the range 0 to m^2.

import math, random
cdef int a,b,c,d,m,pos,value, cyclelimit, nohashcalls   
def h3(int a,int b,int c,int d, int m,int x):
    return (a*x**2 + b*x+c) %m    
def floyd(inputx):
    dupefound, nohashcalls = (0,0)
    m = len(inputx)
    loops = int(m*math.log(m))
    for loopno in xrange(loops):
        if (dupefound == 1):
            break
        a = random.randrange(m)
        b = random.randrange(m)
        c = random.randrange(m)
        d = random.randrange(m)
        pos = random.randrange(m)
        value = inputx[pos]
        listofpos = [0] * m
        listofpos[pos] = 1
        setofvalues = set([value])
        cyclelimit = int(math.sqrt(m))
        for j in xrange(cyclelimit):
            pos = h3(a,b, c,d, m, inputx[pos])
            nohashcalls += 1    
            if (inputx[pos] in setofvalues):
                if (listofpos[pos]==1):
                    dupefound = 0
                else:
                    dupefound = 1
                    print "Duplicate found at position", pos, " and value", inputx[pos]
                break
            listofpos[pos] = 1
            setofvalues.add(inputx[pos])
    return dupefound, nohashcalls 

How can I convert inputx and listofpos to use C type arrays and to access the arrays at C speed? Are there any other speed ups I can use? Can setofvalues be sped up?

So that there is something to compare against, 50 calls to floyd() with m = 5000 currently takes around 30 seconds on my computer.

Update: Example code snippet to show how floyd is called.

m = 5000
inputx = random.sample(xrange(m**2), m)
(dupefound, nohashcalls) = edcython.floyd(inputx)
  • 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-06-16T04:16:37+00:00Added an answer on June 16, 2026 at 4:16 am

    First of all, it seems that you must type the variables inside the function. A good example of it is here.

    Second, cython -a, for “annotate”, gives you a really excellent break down of the code generated by the cython compiler and a color-coded indication of how dirty (read: python api heavy) it is. This output is really essential when trying to optimize anything.

    Third, the now famous page on working with Numpy explains how to get fast, C-style access to the Numpy array data. Unforunately it’s verbose and annoying. We’re in luck however, because more recent Cython provides Typed Memory Views, which are both easy to use and awesome. Read that entire page before you try to do anything else.

    After ten minutes or so I came up with this:

    # cython: infer_types=True
    
    # Use the C math library to avoid Python overhead.
    from libc cimport math
    # For boundscheck below.
    import cython
    # We're lazy so we'll let Numpy handle our array memory management.
    import numpy as np
    # You would normally also import the Numpy pxd to get faster access to the Numpy
    # API, but it requires some fancier compilation options so I'll leave it out for
    # this demo.
    # cimport numpy as np
    
    import random
    
    # This is a small function that doesn't need to be exposed to Python at all. Use
    # `cdef` instead of `def` and inline it.
    cdef inline int h3(int a,int b,int c,int d, int m,int x):
        return (a*x**2 + b*x+c) % m
    
    # If we want to live fast and dangerously, we tell cython not to check our array
    # indices for IndexErrors. This means we CAN overrun our array and crash the
    # program or screw up our stack. Use with caution. Profiling suggests that we
    # aren't gaining anything in this case so I leave it on for safety.
    # @cython.boundscheck(False)
    # `cpdef` so that calling this function from another Cython (or C) function can
    # skip the Python function call overhead, while still allowing us to use it from
    # Python.
    cpdef floyd(int[:] inputx):
        # Type the variables in the scope of the function.
        cdef int a,b,c,d, value, cyclelimit
        cdef unsigned int dupefound = 0
        cdef unsigned int nohashcalls = 0
        cdef unsigned int loopno, pos, j
    
        # `m` has type int because inputx is already a Cython memory view and
        # `infer-types` is on.
        m = inputx.shape[0]
    
        cdef unsigned int loops = int(m*math.log(m))
    
        # Again using the memory view, but letting Numpy allocate an array of zeros.
        cdef int[:] listofpos = np.zeros(m, dtype=np.int32)
    
        # Keep this random sampling out of the loop
        cdef int[:, :] randoms = np.random.randint(0, m, (loops, 5)).astype(np.int32)
    
        for loopno in range(loops):
            if (dupefound == 1):
                break
    
            # From our precomputed array
            a = randoms[loopno, 0]
            b = randoms[loopno, 1]
            c = randoms[loopno, 2]
            d = randoms[loopno, 3]
            pos = randoms[loopno, 4]
    
            value = inputx[pos]
    
            # Unforunately, Memory View does not support "vectorized" operations
            # like standard Numpy arrays. Otherwise we'd use listofpos *= 0 here.
            for j in range(m):
                listofpos[j] = 0
    
            listofpos[pos] = 1
            setofvalues = set((value,))
            cyclelimit = int(math.sqrt(m))
            for j in range(cyclelimit):
                pos = h3(a, b, c, d, m, inputx[pos])
                nohashcalls += 1
                if (inputx[pos] in setofvalues):
                    if (listofpos[pos]==1):
                        dupefound = 0
                    else:
                        dupefound = 1
                        print "Duplicate found at position", pos, " and value", inputx[pos]
                    break
                listofpos[pos] = 1
                setofvalues.add(inputx[pos])
        return dupefound, nohashcalls
    

    There are no tricks here that aren’t explained on docs.cython.org, which is where I learned them myself, but helps to see it all come together.

    The most important changes to your original code are in the comments, but they all amount to giving Cython hints about how to generate code that doesn’t use the Python API.

    As an aside: I really don’t know why infer_types is not on by default. It lets the compiler
    implicitly use C types instead of Python types where possible, meaning less work for you.

    If you run cython -a on this, you’ll see that the only lines that call into Python are your calls to random.sample, and building or adding to a Python set().

    On my machine, your original code runs in 2.1 seconds. My version runs in 0.6 seconds.

    The next step is to get random.sample out of that loop, but I’ll leave that to you.

    I have edited my answer to demonstrate how to precompute the rand samples. This brings the time down to 0.4 seconds.

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

Sidebar

Related Questions

I have a js function that is named getID which is basically return document.getElementById(id)
I have a very simple function which takes in a matching bitfield, a grid,
I have a php application that makes use of a Listener class, which basically
hi i have function which is called by tinker listbox so i cannot return
Can I´m asking for advice. I have function which should return javascript object function
I have a function which looks something like this, it returns a noncopyable class
I have a function which I have to loop it a few times but
I have a function which takes two arguments, the ID of an item (wine)
I have a function which takes several boolean template arguments: template<bool par1, bool par2,
I have a function which zips files and downloads them to the users machine.

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.