Problem with Threads

Discussion in 'Python' started by Kwnstantinos Euaggelidis, Dec 19, 2012.

  1. I have this code for Prime Numbers and i want to do it with Threads.. Any idea.??

    # prime numbers are only divisible by unity and themselves
    # (1 is not considered a prime number by convention)
    import time
    def isprime(n):
    if n == 2:
    return 1
    if n % 2 == 0:
    return 0
    max = n**0.5+1
    i = 3
    while i <= max:
    if n % i == 0:
    return 0
    i+=2
    return 1
    print "Please give the maximum number"
    endnum = input()
    start = time.time()
    for i in range(endnum):
    if isprime(i) == 1:
    print "Number %d is PRIME" % i
    print "Elapsed Time: %s" % (time.time() - start)
     
    Kwnstantinos Euaggelidis, Dec 19, 2012
    #1
    1. Advertising

  2. Kwnstantinos Euaggelidis

    Dave Angel Guest

    On 12/19/2012 12:11 PM, Kwnstantinos Euaggelidis wrote:
    > I have this code for Prime Numbers and i want to do it with Threads.. Any idea.??


    Why do you want to do it with threads? Is it to speed up the
    processing? (Guessing that partly because of your printing "elapsed
    time." Chances are running this code in multiple threads will be
    substantially slower.

    If you want speed, you could speed up the algorithm by a few orders of
    magnitude. Ask for suggestions.

    On the other hand, you might be interested in deciding/discussing the
    tradeoffs of implementing an arbitrary algorithm serially or in
    parallel, using multiple threads, and/or multiple processes, and/or
    multiple networked computers, and/or different possible languages.

    So, if you give us some ideas about your goals, I'm sure many people
    have ideas to share.



    --

    DaveA
     
    Dave Angel, Dec 19, 2012
    #2
    1. Advertising

  3. Kwnstantinos Euaggelidis

    Hans Mulder Guest

    On 19/12/12 18:11:37, Kwnstantinos Euaggelidis wrote:
    > I have this code for Prime Numbers and i want to do it with Threads..
    > Any idea.??


    Why would you want to do that?

    It's not going to be any faster, since your code is CPU-bound.
    You may have several CPUs, but CPython is going to use only one of them.

    If you want to make it faster, read
    http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

    > # prime numbers are only divisible by unity and themselves
    > # (1 is not considered a prime number by convention)
    > import time
    > def isprime(n):
    > if n == 2:
    > return 1
    > if n % 2 == 0:
    > return 0
    > max = n**0.5+1
    > i = 3
    > while i <= max:
    > if n % i == 0:
    > return 0
    > i+=2
    > return 1
    > print "Please give the maximum number"
    > endnum = input()
    > start = time.time()
    > for i in range(endnum):
    > if isprime(i) == 1:
    > print "Number %d is PRIME" % i
    > print "Elapsed Time: %s" % (time.time() - start)


    Hope this helps,

    -- HansM
     
    Hans Mulder, Dec 19, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yoda
    Replies:
    2
    Views:
    471
    =?utf-8?Q?Bj=C3=B6rn_Lindstr=C3=B6m?=
    Aug 1, 2005
  2. threads without threads

    , Aug 27, 2004, in forum: C Programming
    Replies:
    4
    Views:
    436
    William Ahern
    Aug 27, 2004
  3. Pedro Pinto

    Java Threads - Get running threads

    Pedro Pinto, Apr 8, 2008, in forum: Java
    Replies:
    2
    Views:
    1,506
    Arne Vajhøj
    Apr 9, 2008
  4. Une bévue
    Replies:
    0
    Views:
    177
    Une bévue
    Jun 14, 2006
  5. prameela

    Is it a gtk2 problem or Threads problem

    prameela, Mar 19, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    87
    prameela
    Mar 19, 2006
Loading...

Share This Page