Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #35141 > unrolled thread

Problem with Threads

Started byKwnstantinos Euaggelidis <euaggelidiskwn@gmail.com>
First post2012-12-19 09:11 -0800
Last post2012-12-19 19:31 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Problem with Threads Kwnstantinos Euaggelidis <euaggelidiskwn@gmail.com> - 2012-12-19 09:11 -0800
    Re: Problem with Threads Dave Angel <d@davea.name> - 2012-12-19 13:23 -0500
    Re: Problem with Threads Hans Mulder <hansmu@xs4all.nl> - 2012-12-19 19:31 +0100

#35141 — Problem with Threads

FromKwnstantinos Euaggelidis <euaggelidiskwn@gmail.com>
Date2012-12-19 09:11 -0800
SubjectProblem with Threads
Message-ID<0c84b88a-bb71-4ccd-bd96-a3a3414a8e6b@googlegroups.com>
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)

[toc] | [next] | [standalone]


#35146

FromDave Angel <d@davea.name>
Date2012-12-19 13:23 -0500
Message-ID<mailman.1067.1355941437.29569.python-list@python.org>
In reply to#35141
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

[toc] | [prev] | [next] | [standalone]


#35149

FromHans Mulder <hansmu@xs4all.nl>
Date2012-12-19 19:31 +0100
Message-ID<50d207e6$0$6882$e4fe514c@news2.news.xs4all.nl>
In reply to#35141
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web