Why a loop FOR faster than 10 FOR together

Asked

Viewed 226 times

6

I made a for alone count up to 1000000 (sending 1 message each loop) and it took 14 seconds.

public class main 
{
public static void main(String[] args) throws InterruptedException
{
    long init  = System.currentTimeMillis(); 

    Thread ataque = ataque();
    ataque.start();
    ataque.join();

    long end  = System.currentTimeMillis(); 
    long diff = end - init;
    System.out.println("Demorou " + (diff / 1000) + " segundos");
}

static int limite = 1000000;
static int for1 = 0;
static int for2 = (limite / 10);
static int for3 = for2 + for2; 
static int for4 = for3 + for2; 
static int for5 = for4 + for2; 
static int for6 = for5 + for2;
static int for7 = for6 + for2;
static int for8 = for7 + for2;
static int for9 = for8 + for2;

public static Thread ataque()
{
    Thread t = new Thread() 
    { 
        @Override 
        public void run() 
        {
            for(int i = 0; i <= limite; i++)
            {   
                System.out.println(i + " de " + limite); 
            }
        }
    };
    return t;
}
}

Right after I split these for in 10 different simultaneously running 10000000 (sending 1 message each loop) of loops, took 15 seconds.

public class main 
{
public static void main(String[] args) throws InterruptedException
{
    long init  = System.currentTimeMillis(); 
    Thread t1 = ataque1();
    Thread t2 = ataque2();
    Thread t3 = ataque3();
    Thread t4 = ataque4();
    Thread t5 = ataque5();
    Thread t6 = ataque6();
    Thread t7 = ataque7();
    Thread t8 = ataque8();
    Thread t9 = ataque9();
    t1.start();
    t2.start();
    t3.start();
    t4.start();
    t5.start();
    t6.start();
    t7.start();
    t8.start();
    t9.start();
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    t5.join();
    t6.join();
    t7.join();
    t8.join();
    t9.join();

    long end  = System.currentTimeMillis(); 
    long diff = end - init;
    System.out.println("Demorou " + (diff / 1000) + " segundos");
}

static int limite = 1000000;
static int for1 = 0;
static int for2 = (limite / 10);
static int for3 = for2 + for2; 
static int for4 = for3 + for2; 
static int for5 = for4 + for2; 
static int for6 = for5 + for2;
static int for7 = for6 + for2;
static int for8 = for7 + for2;
static int for9 = for8 + for2;

public static Thread ataque1()
{
    Thread t = new Thread() 
    { 
        @Override 
        public void run() 
        {
            for(int i = for1 ; i < for2; i++)
            {
                System.out.println(i + " de " + limite); 
            }
        }
    };
    return t;
}
public static Thread ataque2()
{
    Thread t = new Thread() 
    { 
        @Override 
        public void run() 
        {   
            for(int i = for2; i < for3; i++)
            {
                System.out.println(i + " de " + limite); 
            }
        }
    };
    return t;

}
public static Thread ataque3()
{
    Thread t = new Thread() 
    { 
        @Override 
        public void run() 
        {
            for(int i = for3; i < for4; i++)
            {
                System.out.println(i + " de " + limite); 
            }
        }
    };
    return t;
}
public static Thread ataque4()
{
    Thread t = new Thread() 
    { 
        @Override 
        public void run() 
        {
            for(int i = for4; i <= for5; i++)
            {   
                System.out.println(i + " de " + limite); 
            }
        }
    };
    return t;
}
public static Thread ataque5()
{
    Thread t = new Thread() 
    { 
        @Override 
        public void run() 
        {
            for(int i = for5; i < for6; i++)
            {
                System.out.println(i + " de " + limite); 
            }
        }
    };
    return t;
}
public static Thread ataque6()
{
    Thread t = new Thread() 
    { 
        @Override 
        public void run() 
        {
            for(int i = for6; i < for7; i++)
            {
                System.out.println(i + " de " + limite); 
            }
        }
    };
    return t;
}
public static Thread ataque7()
{
    Thread t = new Thread() 
    { 
        @Override 
        public void run() 
        {
            for(int i = for7; i < for8; i++)
            {
                System.out.println(i + " de " + limite); 
            }
        }
    };
    return t;
}
public static Thread ataque8()
{
    Thread t = new Thread() 
    { 
        @Override 
        public void run() 
        {
            for(int i = for8; i < for9; i++)
            {
                System.out.println(i + " de " + limite); 
            }
        }
    };
    return t;
}
public static Thread ataque9()
{
    Thread t = new Thread() 
    { 
        @Override 
        public void run() 
        {
            for(int i = for9; i < limite; i++)
            {
                System.out.println(i + " de " + limite); 
            }
        }
    };
    return t;
}
}

Why even though he is "10 times more efficient" continues to give a worse result?

  • This is not how @Lucascarezia Each Thread can only be run by one processor. Do you have 10 processors on your machine? Read this answer: http://answall.com/questions/70769/multi-core-cpus-por-que-minha-aplica%C3%A7%C3%A3o-n%C3%A3o-utiliza-todos-os-n%C3%Bacleos-do-proces/70989#70989 Recomendo tb que vc de uma revisada em computacão paralela.

  • 1

    Not always can an algorithm be faster through the use of parallel computing, there are several factors that will be involved to determine how much faster a program can be: https://en.wikipedia.org/wiki/Amdahl%27s_law

1 answer

11


Basically you need to read this question/answer to understand.

In a lot of situations splitting up the work actually slows down. There is no miracle, if you do not have the resources for the execution to be effectively parallel, you will only gain a work overload to manage the various tasks being switched (parallelizing will always have this cost, but if you have more processors and you don’t have a dependency, it just won’t be linear).

Other than that, I don’t really know how to do it benchmark in Java, I don’t know if this way of measuring time is the most recommended. I have seen many tests produce wrong results because the shape of the measurement was wrong.

  • Thank you, clarified my doubts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.