Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Problem with Threads

Reply
Thread Tools

Problem with Threads

 
 
Kwnstantinos Euaggelidis
Guest
Posts: n/a
 
      12-19-2012
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)
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      12-19-2012
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

 
Reply With Quote
 
 
 
 
Hans Mulder
Guest
Posts: n/a
 
      12-19-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Threads - Get running threads Pedro Pinto Java 2 04-08-2008 11:44 PM
[new to threads] threads with UI and loop Une bévue Ruby 0 06-14-2006 10:22 AM
TB View, Threads, Threads with unread The Invisible Man Firefox 1 03-20-2006 02:09 AM
Standard Threads vs Weightless Threads yoda Python 2 08-01-2005 09:12 PM
threads without threads sindica@gmail.com C Programming 4 08-27-2004 09:25 PM



Advertisments