70 lines
1.2 KiB
Java
70 lines
1.2 KiB
Java
package UEB01;
|
|
|
|
public class Zeitmessung
|
|
{
|
|
static int tuWasCounter = 0;
|
|
private static double tuwas()
|
|
{
|
|
tuWasCounter++;
|
|
return Math.random();
|
|
}
|
|
|
|
// Linear
|
|
public static double func1(int n)
|
|
{
|
|
double summe = 0;
|
|
|
|
for (int a = 0; a < n; a++)
|
|
summe += tuwas();
|
|
|
|
return summe;
|
|
}
|
|
|
|
// Quadratisch
|
|
public static double func2(int n)
|
|
{
|
|
double summe = 0;
|
|
|
|
for (int a = 0; a < n; a++)
|
|
for(int b = 0; b < n; b++)
|
|
summe += tuwas();
|
|
|
|
return summe;
|
|
}
|
|
|
|
// log2(n)
|
|
public static double func6(int n)
|
|
{
|
|
double summe = 0;
|
|
|
|
while (n > 0)
|
|
{
|
|
summe += tuwas();
|
|
n /= 2;
|
|
}
|
|
|
|
return summe;
|
|
}
|
|
|
|
public static void main(String[] args)
|
|
{
|
|
StopUhr uhr = new StopUhr();
|
|
final int n = 100000;
|
|
tuWasCounter = 0;
|
|
uhr.start();
|
|
func1(n);
|
|
uhr.stop();
|
|
System.out.println("func1 "+"n="+n+" Zeit:"+uhr.getDuration()+" tuWasCounter:" + tuWasCounter);
|
|
tuWasCounter = 0;
|
|
uhr.start();
|
|
func2(n);
|
|
uhr.stop();
|
|
System.out.println("func2 "+"n="+n+" Zeit:"+uhr.getDuration()+" tuWasCounter:" + tuWasCounter);
|
|
tuWasCounter = 0;
|
|
uhr.start();
|
|
func6(n);
|
|
uhr.stop();
|
|
System.out.println("func6 "+"n="+n+" Zeit:"+uhr.getDuration()+" tuWasCounter:" + tuWasCounter);
|
|
}
|
|
}
|