Dual Core outlook
Tom Anderson
twic at urchin.earth.li
Tue Feb 7 15:35:08 EST 2006
On Tue, 7 Feb 2006, malv wrote:
> Maybe this is too simplistic, but given two programs, one in Python the
> other in Java or C#. Would this mean that running the latter on a dual
> core processor would significantly increase execution speed, whereas the
> Python program would be running in one processor only without any speed
> up?
>
> Is a Java program capable of this "out of the box" or does this require
> specific additional code?
If i understand the question correctly, the latter - you need to take
explicit action to make use of two processors in Java, or any other
mainstream language.
What i think you're asking is whether code like this:
public class MyApp
{
public static void main(String[] args)
{
// do a bunch of stuff
}
}
will run faster with two cores. The answer to that is definitely no (more
or less - see below!); this program has one thread of execution, and each
thread can only use one core at a time, so this program will only use one
core. In order to use multiple cores, you need multiple threads:
public class MyApp implements Runnable
{
public static final int NUM_THREADS = 4 ; // or whatever
public static void main(String[] args)
{
for (int i = 0 ; i < NUM_THREADS ; ++i)
new Thread(new MyApp()).start() ;
}
public void run()
{
// do a bunch of stuff
}
}
This program will get a roughly proportional speedup from multiple cores,
provided the actual work can be done without the threads threading on each
others' toes. If you try the equivalent in python (using the threading
module), you, AIUI, won't get a significant speedup, as python is
essentially single-threaded (is that really true?).
Now, as i promised above, the thing about single-threaded programs not
getting a speedup from multiple cores is not quite right. The thing is, a
program in which a single thread is executing your code is not actually
single-threaded - there are other threads running in the background, doing
mysterious system stuff; with the Sun 1.5.0 JVM on Windows XP, a minimal
program has eight threads running. One of the things those threads are
doing - or will be once JVMs start to adapt to the multi-core world - is
garbage collection; GC can eat a measurable, although not huge, fraction
of your execution time, so farming it out to a second core should speed
your program up a bit.
tom
PS Excuse any errors in the java - it's a long time since i've written
any!
--
Through the darkness of Future Past the magician longs to see.
More information about the Python-list
mailing list