HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Calculation of e performance issue

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
issuecalculationperformance

Problem

Can you give me some idea how to optimize the switch statements, maybe a for or do while loop? Which is faster, switch or for/while loop?

```
public class CalculateTheValueOfE {

static long start1 = System.currentTimeMillis();
static int numberOfThread = 4;
private static int precision = 0;
private static int threadCount = 0;
private static String defaultFile = null;

static BigDecimal result = BigDecimal.ONE;

public static BigDecimal factorial(int x) {
BigDecimal prod = new BigDecimal("1");
for (int i = x; i > 1; i--) {
prod = prod.multiply(new BigDecimal(i));
}
return prod;
}

public static Runnable getRunner(final int from, final int to) {
Runnable runner = new Runnable() {
@Override
public void run() {
BigDecimal e = BigDecimal.valueOf(0);
for (int i = from; i < to; i++) {
e = e.add(BigDecimal.valueOf(3 3 i i + 1).divide(factorial(3 i), new MathContext(1000, RoundingMode.HALF_UP)));
}
addResult(e);
}
};
return runner;
}

public static synchronized void addResult(BigDecimal e) {
result = result.add(e);
}

public static void main(String[] args) throws InterruptedException, IOException {
//Thread tr[] = new Thread[threadCount];
// Runnable r[] = new Runnable[numberOfThread];

File file = new File("result.txt");
FileOutputStream fis = new FileOutputStream(file);
PrintStream out = new PrintStream(fis);
System.setOut(out);
for (int i = 0; i < args.length; i++) {

System.out.println(i + " " + args[i]);

}
threadCount = Integer.valueOf(args[1]);
precision = Integer.valueOf(args[3]);
defaultFile = args[5];
System.out.println("Number of threads: " + threadCount + " precision: " + precision + " default

Solution

Which is faster, switch or for/while loop?

The speed difference for you here regarding a switch or a for/while loop is nothing compared to the rest of your code. You should really not be worried about that here.

That being said, I believe a switch is technically faster, but as I said. You shouldn't worry about that there!

You should be worried about maintainability, flexibility and readability. In which case a for-loop is substantively better.
Unexpected Results without Error Messages

Imagine the user currently inputting "10" as the number of threads. Then what happens? E becomes 1. No error message. This can be very confusing for a user (Based on a true story). Your current switch approach does not support that option.

To fix this, either a) Add a default statement to your switch, stating that the chosen number of threads is not a valid option. b) Add support for it by using a for-loop instead!
Get rid of the static habit!

Instead of getting used to static, create an instance of your class and pass the required variables to it's constructor (such as precision and threadCount) and then use a non-static method to make the computations.

I think that the reason you're using static is that it's the "easiest/fastest" approach. But I'm warning you: It might come around and haunt you. I recommend avoiding over-using static.

By using the non-static approach and passing arguments to the constructor, you can mark several fields as final which is considered a good practice when possible.
Unused variables

static int numberOfThread = 4;
int calculation = precision / threadCount;


These variables does not seem to be used. Remove them.
Other comments

  • Use BigDecimal.ONE instead of new BigDecimal("1")



  • Instead of computing time with System.currentTimeMillis(); use System.nanoTime();



  • Create and return on same line. Instead of Runnable runner = new Runnable... you can return new Runnable...



  • You have some commented code without explaining why it is commented. Remove that code next time before posting it for review to reduce confusion.



Once again though, most importantly:
Don't use switch here, use for!

(See answer by @VoiceOfUnreason)

Code Snippets

static int numberOfThread = 4;
int calculation = precision / threadCount;

Context

StackExchange Code Review Q#53821, answer score: 7

Revisions (0)

No revisions yet.