3

I am just learning about multithreading by trying to create some little programs, and I found one specific thing I am not sure why it is the way it is.

I've two classes, they both count to 2 billion, and at the end they print out time it took them to do it. They are in separate files. The First one can do it in about 2 seconds (It would be faster but I am also doing other stuff there), and the new Thread(new Runnable())

in main class is much slower, it took about 8 seconds to do it.Can you explain why? Here is the code. Thank you.

public class First implements Runnable {

private long startTime, endTime;
public static double count = 0;

@Override
public void run() {
    startTime = System.currentTimeMillis();

    for (int count = 0; count <= 2000000000L; count++);

    endTime = System.currentTimeMillis();
    System.out.println(endTime - startTime); //it is done in about 2seconds
  }
 }


public class Threads {

   public static void main(String[] args){

         First f = new First();
         f.run();

     new Thread(new Runnable() {

        @Override
        public void run() {

            long startTime, endTime;
            double count;
            startTime = System.currentTimeMillis();

            for (count = 0; count < 2000000000L; count++)l

            endTime = System.currentTimeMillis();
            System.out.println(endTime - startTime); //about 8 seconds

        }
    }).start();
 }
}

Update

Problem was on line 4, I did not realized that variable count is double, and on line 10, in for cycle I used int. Speed of incrementing the variables has nothing to do with threads, It is the problem of primitive data types as mentioned in comments down below.

hocikto
  • 871
  • 1
  • 14
  • 29
  • 8
    The difference is that one of the `i` variables is a `double` and the other is an `int`. –  May 24 '13 at 18:13
  • 3
    To add onto that comment, I believe you meant to use the class field `i` as defined on line 4. Just remove the `int` token in the loop on line 11. –  May 24 '13 at 18:15
  • Oh my god :D I did not realize it, it really is because of it, thanks, and sorry for creating such a stupid question – hocikto May 24 '13 at 18:17
  • It's no problem. It took me one or two seconds to really find that small bug. Tricky little guy there haha –  May 24 '13 at 18:18

2 Answers2

5

That has nothing to do with threads. Look carefully at your code (it's a mess by the way), first example uses int i as loop index, second uses a double i as loop index. Double is a different data type and generally much slower than int.

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
Durandal
  • 19,919
  • 4
  • 36
  • 70
  • I know it does not look very good there is a little more of the code on my computer I just copied the important parts of it. At least I learned something new, thank you. – hocikto May 24 '13 at 18:26
  • 1
    @MartinŠvoňava you should use words for variables instead of single letters, because those letters mean nothing. In particular, if you are going to ask others for help, standardizing your code and using meaningful variable names greatly speeds up the process of others understanding your code. For a personal project I can understand, but "prettying up" the code before posting online really does help us and future readers. – kurtzbot May 24 '13 at 18:28
  • I wasm't referring to the formatting, although thats distracting here, too. What I find messy is declaring single letter instance members and using them in place of local variables (private long x, z) and you probably intended to use the class member "static double i" somewhere, too. – Durandal May 24 '13 at 18:32
  • +1 kurtbot And to reenforce that point, "prettying up" often already finds the problem (at least for me it often did). – Durandal May 24 '13 at 18:34
0

One possibility is that in your second example, your counter is a double rather than an integer. Depending on the architecture and particular operation, integers and doubles have different performance. Incrementing the value might be a special case, but it's possible that on your machine, incrementing doubles is slower than incrementing integers. Try replacing double i with int i and see how it performs.

Source: https://stackoverflow.com/a/2550851/2076603

Community
  • 1
  • 1
James T
  • 607
  • 6
  • 9
  • Yes, I found out int is much faster, I used double for the first time because I wanted to count to 20 billion not 2, so there was a mistake. – hocikto May 24 '13 at 18:27