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

  • Home
  • SEARCH
  • 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 9164053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:35:20+00:00 2026-06-17T14:35:20+00:00

It is my understanding that the itertools functions are written in C . If

  • 0

It is my understanding that the itertools functions are written in C. If i wanted to speed this example code up:

import numpy as np
from itertools import combinations_with_replacement

def combinatorics(LargeArray):
     newArray = np.empty((LargeArray.shape[0],LargeArray.shape[0]))
     for x, y in combinations_with_replacement(xrange(LargeArray.shape[0]), r=2):
         z = LargeArray[x] + LargeArray[y]
         newArray[x, y] = z
     return newArray

Since combinations_with_replacement is written in C, does that imply that it can’t be sped up? Please advise.

Thanks in advance.

  • 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-17T14:35:22+00:00Added an answer on June 17, 2026 at 2:35 pm

    It’s true that combinations_with_replacement is written in C, which means that you’re not likely to speed up the implementation of that part of the code. But most of your code isn’t spent on finding the combinations: it’s on the for loop that does the additions. You really, really, really want to avoid that kind of loop if at all possible when you’re using numpy. This version will do almost the same thing, through the magic of broadcasting:

    def sums(large_array):
        return large_array.reshape((-1, 1)) + large_array.reshape((1, -1))
    

    For example:

    >>> ary = np.arange(5).astype(float)
    >>> np.triu(combinatorics(ary))
    array([[ 0.,  1.,  2.,  3.,  4.],
           [ 0.,  2.,  3.,  4.,  5.],
           [ 0.,  0.,  4.,  5.,  6.],
           [ 0.,  0.,  0.,  6.,  7.],
           [ 0.,  0.,  0.,  0.,  8.]])
    >>> np.triu(sums(ary))
    array([[ 0.,  1.,  2.,  3.,  4.],
           [ 0.,  2.,  3.,  4.,  5.],
           [ 0.,  0.,  4.,  5.,  6.],
           [ 0.,  0.,  0.,  6.,  7.],
           [ 0.,  0.,  0.,  0.,  8.]])
    

    The difference is that combinatorics leaves the lower triangle as random gibberish, where sums makes the matrix symmetric. If you really wanted to avoid adding everything twice, you probably could, but I can’t think of how to do it off the top of my head.

    Oh, and the other difference:

    >>> big_ary = np.random.random(1000)
    >>> %timeit combinatorics(big_ary)
    1 loops, best of 3: 482 ms per loop
    >>> %timeit sums(big_ary)
    1000 loops, best of 3: 1.7 ms per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the understanding that using data access routines directly from presentation code is
It is my understanding that although Mercurial has support from branches, the community generally
It's my understanding that, in terms of selector speed, that #ID selectors are fastest,
From a little bit of reading around, it is my understanding that the only
Its my understanding that the questions in StackOverflow has the following format http://stackoverflow.com/questions/{question-id}/{slug-made-from-question-title} So
Already understanding that AES is the encryption method of choice, should existing code that
It is my understanding that you can return an array from a function in
Am I right understanding that def is evaluated every time it gets accessed lazy
It's my understanding that GHC gives each thread a stack. Why is this necessary?
While it's my understanding that there's no fundamental reason a program written for 32-bit

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.