Reputation: 31
I'm doing a science fair project on how adding cores to a processor affects computing speed. I programmed my own benchmark to do this in Java with the Eclipse platform. My problem is that when the program runs, there is very little difference between the time it takes the computer when it is only using 1 processing core versus how long it takes when it is using all 6. Is there a way I can get the program to make use of additional processing cores so that it runs faster on 6 cores? Here is a copy of the code:
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.text.*;
public class SciFair
{
/**
* N. Roberts
* 1/13/11
* Science Fair 2011
*/
public static String now(String dateFormat) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
return sdf.format(cal.getTime());
}
public static void main(String[] args)
{
Scanner dataIn = new Scanner(System.in);
DecimalFormat timeInSeconds = new DecimalFormat("##.###");
System.out.println("Are you ready to begin the benchmark? ");
String response = dataIn.nextLine();
double num = 5832544225416546445465465465465465448412168546725.2655236355335649499923164684654345649874181221173246189579162421579584572121334216957951462175918894199993642446445548521652158975;
double endNum = 0;
if(response.equals("Yes") || response.equals("yes") || response.equals("Ok") || response.equals("ok"))
{
System.out.println("Benchmark starting at " + SciFair.now("H:mm:ss:SSS") + ".");
String startTimeHours = SciFair.now("H");
String startTimeMinutes = SciFair.now("mm");
String startTimeSeconds = SciFair.now("ss");
String startTimeMilliseconds = SciFair.now("SSS");
double startTimeHoursNumFormat = Double.valueOf(startTimeHours.trim()).doubleValue();
double startTimeMinutesNumFormat = Double.valueOf(startTimeMinutes.trim()).doubleValue();
double startTimeSecondsNumFormat = Double.valueOf(startTimeSeconds.trim()).doubleValue();
double startTimeMillisecondsNumFormat = Double.valueOf(startTimeMilliseconds.trim()).doubleValue();
double startHoursInSeconds = (startTimeHoursNumFormat * 3600);
double startMinutesInSeconds = (startTimeMinutesNumFormat * 60);
double startMillisecondsInSeconds = (startTimeMillisecondsNumFormat / 1000);
double startTotalSeconds = (startHoursInSeconds + startMinutesInSeconds + startTimeSecondsNumFormat + startMillisecondsInSeconds);
for(double numberRun = 0; numberRun <= 1000000000; numberRun++)
{
endNum = endNum+((num * 58445246411658545558.458561435435385465146445641894984984651321616548946132131654984 + 26564.6351648941654984156 / 321654984616321654654984365.3216548974165416484 + 216541648165141654.23164584961321316546844651 * 65616484316541486541.1654484646546576746862468546576248645814668585145181 / 111.3216516543464545795479884754424759845 * 3467247954975472171276545789595.4721756147541715715157145715145714514748717521 / 31654688498445132154.3215449848489461531654 * 61654984132165465484464131321145644684.3164846434654648446546546465456465465414321165118547521569845258547566352544 / 32136546465434654654484843131.3164484151515121211554464547951126758178571454164175415417584175664) / 2431276251975915431326725157958456157321624621765419.4321672175951247316726451751487465462645754579517232172757497516373127651975167516715);
}
System.out.println("\nBenchmark is 10% complete.");
for(double numberRun = 0; numberRun <= 1000000000; numberRun++)
{
endNum = endNum+((num * 58445246411658545558.458561435435385465146445641894984984651321616548946132131654984 + 26564.6351648941654984156 / 321654984616321654654984365.3216548974165416484 + 216541648165141654.23164584961321316546844651 * 65616484316541486541.1654484646546576746862468546576248645814668585145181 / 111.3216516543464545795479884754424759845 * 3467247954975472171276545789595.4721756147541715715157145715145714514748717521 / 31654688498445132154.3215449848489461531654 * 61654984132165465484464131321145644684.3164846434654648446546546465456465465414321165118547521569845258547566352544 / 32136546465434654654484843131.3164484151515121211554464547951126758178571454164175415417584175664) / 2431276251975915431326725157958456157321624621765419.4321672175951247316726451751487465462645754579517232172757497516373127651975167516715);
}
System.out.println("Benchmark is 20% complete..");
for(double numberRun = 0; numberRun <= 1000000000; numberRun++)
{
endNum = endNum+((num * 58445246411658545558.458561435435385465146445641894984984651321616548946132131654984 + 26564.6351648941654984156 / 321654984616321654654984365.3216548974165416484 + 216541648165141654.23164584961321316546844651 * 65616484316541486541.1654484646546576746862468546576248645814668585145181 / 111.3216516543464545795479884754424759845 * 3467247954975472171276545789595.4721756147541715715157145715145714514748717521 / 31654688498445132154.3215449848489461531654 * 61654984132165465484464131321145644684.3164846434654648446546546465456465465414321165118547521569845258547566352544 / 32136546465434654654484843131.3164484151515121211554464547951126758178571454164175415417584175664) / 2431276251975915431326725157958456157321624621765419.4321672175951247316726451751487465462645754579517232172757497516373127651975167516715);
}
System.out.println("Benchmark is 30% complete...");
for(double numberRun = 0; numberRun <= 1000000000; numberRun++)
{
endNum = endNum+((num * 58445246411658545558.458561435435385465146445641894984984651321616548946132131654984 + 26564.6351648941654984156 / 321654984616321654654984365.3216548974165416484 + 216541648165141654.23164584961321316546844651 * 65616484316541486541.1654484646546576746862468546576248645814668585145181 / 111.3216516543464545795479884754424759845 * 3467247954975472171276545789595.4721756147541715715157145715145714514748717521 / 31654688498445132154.3215449848489461531654 * 61654984132165465484464131321145644684.3164846434654648446546546465456465465414321165118547521569845258547566352544 / 32136546465434654654484843131.3164484151515121211554464547951126758178571454164175415417584175664) / 2431276251975915431326725157958456157321624621765419.4321672175951247316726451751487465462645754579517232172757497516373127651975167516715);
}
System.out.println("Benchmark is 40% complete....");
for(double numberRun = 0; numberRun <= 1000000000; numberRun++)
{
endNum = endNum+((num * 58445246411658545558.458561435435385465146445641894984984651321616548946132131654984 + 26564.6351648941654984156 / 321654984616321654654984365.3216548974165416484 + 216541648165141654.23164584961321316546844651 * 65616484316541486541.1654484646546576746862468546576248645814668585145181 / 111.3216516543464545795479884754424759845 * 3467247954975472171276545789595.4721756147541715715157145715145714514748717521 / 31654688498445132154.3215449848489461531654 * 61654984132165465484464131321145644684.3164846434654648446546546465456465465414321165118547521569845258547566352544 / 32136546465434654654484843131.3164484151515121211554464547951126758178571454164175415417584175664) / 2431276251975915431326725157958456157321624621765419.4321672175951247316726451751487465462645754579517232172757497516373127651975167516715);
}
System.out.println("Benchmark is 50% complete.....");
for(double numberRun = 0; numberRun <= 1000000000; numberRun++)
{
endNum = endNum+((num * 58445246411658545558.458561435435385465146445641894984984651321616548946132131654984 + 26564.6351648941654984156 / 321654984616321654654984365.3216548974165416484 + 216541648165141654.23164584961321316546844651 * 65616484316541486541.1654484646546576746862468546576248645814668585145181 / 111.3216516543464545795479884754424759845 * 3467247954975472171276545789595.4721756147541715715157145715145714514748717521 / 31654688498445132154.3215449848489461531654 * 61654984132165465484464131321145644684.3164846434654648446546546465456465465414321165118547521569845258547566352544 / 32136546465434654654484843131.3164484151515121211554464547951126758178571454164175415417584175664) / 2431276251975915431326725157958456157321624621765419.4321672175951247316726451751487465462645754579517232172757497516373127651975167516715);
}
System.out.println("Benchmark is 60% complete......");
for(double numberRun = 0; numberRun <= 1000000000; numberRun++)
{
endNum = endNum+((num * 58445246411658545558.458561435435385465146445641894984984651321616548946132131654984 + 26564.6351648941654984156 / 321654984616321654654984365.3216548974165416484 + 216541648165141654.23164584961321316546844651 * 65616484316541486541.1654484646546576746862468546576248645814668585145181 / 111.3216516543464545795479884754424759845 * 3467247954975472171276545789595.4721756147541715715157145715145714514748717521 / 31654688498445132154.3215449848489461531654 * 61654984132165465484464131321145644684.3164846434654648446546546465456465465414321165118547521569845258547566352544 / 32136546465434654654484843131.3164484151515121211554464547951126758178571454164175415417584175664) / 2431276251975915431326725157958456157321624621765419.4321672175951247316726451751487465462645754579517232172757497516373127651975167516715);
}
System.out.println("Benchmark is 70% complete.......");
for(double numberRun = 0; numberRun <= 1000000000; numberRun++)
{
endNum = endNum+((num * 58445246411658545558.458561435435385465146445641894984984651321616548946132131654984 + 26564.6351648941654984156 / 321654984616321654654984365.3216548974165416484 + 216541648165141654.23164584961321316546844651 * 65616484316541486541.1654484646546576746862468546576248645814668585145181 / 111.3216516543464545795479884754424759845 * 3467247954975472171276545789595.4721756147541715715157145715145714514748717521 / 31654688498445132154.3215449848489461531654 * 61654984132165465484464131321145644684.3164846434654648446546546465456465465414321165118547521569845258547566352544 / 32136546465434654654484843131.3164484151515121211554464547951126758178571454164175415417584175664) / 2431276251975915431326725157958456157321624621765419.4321672175951247316726451751487465462645754579517232172757497516373127651975167516715);
}
System.out.println("Benchmark is 80% complete........");
for(double numberRun = 0; numberRun <= 1000000000; numberRun++)
{
endNum = endNum+((num * 58445246411658545558.458561435435385465146445641894984984651321616548946132131654984 + 26564.6351648941654984156 / 321654984616321654654984365.3216548974165416484 + 216541648165141654.23164584961321316546844651 * 65616484316541486541.1654484646546576746862468546576248645814668585145181 / 111.3216516543464545795479884754424759845 * 3467247954975472171276545789595.4721756147541715715157145715145714514748717521 / 31654688498445132154.3215449848489461531654 * 61654984132165465484464131321145644684.3164846434654648446546546465456465465414321165118547521569845258547566352544 / 32136546465434654654484843131.3164484151515121211554464547951126758178571454164175415417584175664) / 2431276251975915431326725157958456157321624621765419.4321672175951247316726451751487465462645754579517232172757497516373127651975167516715);
}
System.out.println("Benchmark is 90% complete.........");
for(double numberRun = 0; numberRun <= 1000000000; numberRun++)
{
endNum = endNum+((num * 58445246411658545558.458561435435385465146445641894984984651321616548946132131654984 + 26564.6351648941654984156 / 321654984616321654654984365.3216548974165416484 + 216541648165141654.23164584961321316546844651 * 65616484316541486541.1654484646546576746862468546576248645814668585145181 / 111.3216516543464545795479884754424759845 * 3467247954975472171276545789595.4721756147541715715157145715145714514748717521 / 31654688498445132154.3215449848489461531654 * 61654984132165465484464131321145644684.3164846434654648446546546465456465465414321165118547521569845258547566352544 / 32136546465434654654484843131.3164484151515121211554464547951126758178571454164175415417584175664) / 2431276251975915431326725157958456157321624621765419.4321672175951247316726451751487465462645754579517232172757497516373127651975167516715);
}
String endTimeHours = SciFair.now("H");
String endTimeMinutes = SciFair.now("mm");
String endTimeSeconds = SciFair.now("ss");
String endTimeMilliseconds = SciFair.now("SSS");
double endTimeHoursNumFormat = Double.valueOf(endTimeHours.trim()).doubleValue();
double endTimeMinutesNumFormat = Double.valueOf(endTimeMinutes.trim()).doubleValue();
double endTimeSecondsNumFormat = Double.valueOf(endTimeSeconds.trim()).doubleValue();
double endTimeMillisecondsNumFormat = Double.valueOf(endTimeMilliseconds.trim()).doubleValue();
double endHoursInSeconds = (endTimeHoursNumFormat * 3600);
double endMinutesInSeconds = (endTimeMinutesNumFormat * 60);
double endMillisecondsInSeconds = (endTimeMillisecondsNumFormat / 1000);
double endTotalSeconds = (endHoursInSeconds + endMinutesInSeconds + endTimeSecondsNumFormat + endMillisecondsInSeconds);
double elapsedTime = (endTotalSeconds - startTotalSeconds);
System.out.println("\nThe benchmark is complete at " + SciFair.now("H:mm:ss:SSS") + ", and the end result is " + endNum + ".");
System.out.println("\nThe benchmark was run in " + timeInSeconds.format(elapsedTime) + " seconds.");
}
else
{
System.out.println("\nPlease restart the program when you are ready to benchmark.");
}
}
}
Upvotes: 3
Views: 1585
Reputation: 81104
Just an aside: you're over-complicating your timing code a bit. Here's a cleaner way to track the number of seconds that have elapsed:
long start = System.currentTimeMillis();
//do work
long end = System.currentTimeMillis();
double elapsedSeconds = (end - start) / 1000.0;
Then you would only need your now()
function to report the current time.
Upvotes: 1
Reputation: 138922
What you need to do is benchmark a task that is parallelizable. In other words, the task needs to be able to be split up into completely separate subproblems.
For example, you have many for-loops in your benchmark. They are separate problems, and can be split into different threads.
To do this, your code will look something similar to the following:
Thread thread1 = new Thread(){public void run(){
// your for-loop
}};
You will do that for each for-loop, and then, you will call thread1.start();
for each thread.
You will then need to call thread1.join()
on each Thread
to wait for that work to complete.
Your new multi-threaded code will follow the following pattern:
Create all threads;
Start all threads;
Wait for all threads to complete;
Report on time taken to complete all threads;
This benchmark should perform faster based on the number of cores available.
Upvotes: 1
Reputation: 23629
System.currentTimeMillis()
and use a SimpleDateFormat object to output the time in the format you want.Upvotes: 0
Reputation: 4900
Have a look at the Java concurrency tutorial.
Basically, your program is the equivant of one person working on one thing. To reap the benefits of multiple cores, you need multiple threads - the equivant of many people working in parallel on either the same thing or multiple things.
Upvotes: 0
Reputation: 847
In Java, you can do it like this:
Suppose your current main looks something like this:
setUpTimers();
long numberOfLoops=10000L;
doSomethingThatTakesALongTime(numberOfLoops);
reportTimers();
You can recast this as follows:
setUpTimers();
List<Thread> threads = new ArrayList<Thread>();
long numberOfLoops=10000L;
for (int i=0; i<numberOfThreads; ++i) {
Thread t = new Thread() {
public void run() {
doSomethingThatTakesALongTime(numberOfLoops/numberOfThreads);
}
};
t.start();
threads.add(t);
}
for (Thread t : threads){
t.join();
}
reportTimers();
You'll want to make sure, if you do this, that "number of loops" is evenly divisible by "number of threads".
Upvotes: 2
Reputation: 72344
You want to use either the raw thread objects or the classes in java.util.concurrent. Multithreading doesn't come for free!
Upvotes: 0
Reputation: 28084
By using multiple Threads. Those are automatically delegated to the Cores by the JVM.
Edit: See the Lesson: Concurrency in the Java Tutorial. There is too much to say.
Upvotes: 5