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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:39:39+00:00 2026-05-24T02:39:39+00:00

I’m trying to find a way to make the following algorithm being processed on

  • 0

I’m trying to find a way to make the following algorithm being processed on multiple cores, but I don’t get on a good point. Using a locked iterator shared between multiple processes wouldn’t be the most efficient way I think.

 def sortCharset(set):
   _set = ""
   for c in set:
     if c not in _set:
       _set += c
   set = _set
   del _set
   set = list(set)
   set.sort()
   return "".join(set)
 
 
 def stringForInt(num, set, length):
   setLen = len(set)
   string = ""
   string += set[num % setLen]
   for n in xrange(1,length):
     num //= setLen
     string += set[num % setLen]
   return string
 
 
 def bruteforce(set, length, raw = False):
   if raw is False:
     set = sortCharset(set)
 
   for n in xrange(len(set) ** length):
     yield stringForInt(n, set, length)

Short explanation:
The code is used to create every possible combination
from a set of chars, i.e. to hack a password.
(Of course not my intention, just some Py-training. 😉

What is a good way to run this algorithm on multiple cores ?

  • 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-24T02:39:39+00:00Added an answer on May 24, 2026 at 2:39 am

    The question isn’t really about naming style or how to get a sorted set of characters out of a string.

    You might want to look into the multiprocessing module. I’m pretty much a n00b w/r/t multi-core parallelism but got something working:

    import multiprocessing, itertools
    
    def stringForInt(args):
        num, charset, length = args ## hack hack hack
        setlen = len(charset)
        s = []
        s.append(charset[num % setlen])
        for n in xrange(1, length):
            num //= setlen
            s.append(charset[num % setlen])
        return ''.join(s)
    
    def bruteforce(charset, length, mapper, raw=False):
        if not raw:
            charset = sorted(set(charset))
        return mapper(stringForInt, ((n,charset,length) for n in xrange(len(charset)**length)))
    
    if __name__ == '__main__':
        import time, sys
        if len(sys.argv) == 1 or sys.argv[1] == 'map':
            mapper = map
        else:
            p = multiprocessing.Pool()
            pfunc = {'pmap':p.map,
                     'imap':p.imap,
                     'imapu':p.imap_unordered}[sys.argv[1]]
            mapper = lambda f, i: pfunc(f, i, chunksize=5)
        o = bruteforce('abcdefghijk',6,mapper)
        if not isinstance(o, list):
            list(o)
    

    The nature of the hack is that you need to use pickleable objects for the functions in multiprocessing and only functions that are defined at the top-level are can be pickled. (There would be other ways around this using multiprocessing.Value or multiprocessing.Manager but they aren’t really worth going into for present purposes.)

    Here’s output for various runs:

    $ for x in map pmap imap imapu ; do time python mp.py $x; done
    
    real    0m9.351s
    user    0m9.253s
    sys     0m0.096s
    
    real    0m10.523s
    user    0m20.753s
    sys     0m0.176s
    
    real    0m4.081s
    user    0m13.797s
    sys     0m0.276s
    
    real    0m4.215s
    user    0m14.013s
    sys     0m0.236s
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.