I made a for
alone count to 1000000 (sending 1 message to 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;
}
}
Soon after I split these for
into 10 different ones by simultaneously executing 10000000 (sending 1 message every loop ), it 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 is he even "10 times more efficient" still giving a worse result?