Я провел эксперимент с l oop.
Прежде всего, эксперимент: -
У нас есть 2 задачи T1 и T2.
В 1-м В этом случае мы выполняем T1 и T2 в одном l oop. Во втором случае мы выполняем T1 и T2 в отдельном l oop.
. Затем мы видим, какой из них лучше.
Теперь это код (Очень базовый c вещь. ..): -
public class TestClass {
public static void main(String[] args) throws InterruptedException {
long startTime, endTime, totalTime;
int cI, cJ, cK, cL;
int avgRed = 0;
for (int tester = 1; tester <= 25; tester++) {
long AvgNType = 0, AvgNN1Type = 0, AvgNN2Type = 0;
for (int caseNo = 0; caseNo < 1000; caseNo++) {
int temp = 0;
cI = (int) (Math.random() * 499 + 1);
cJ = (int) (Math.random() * 499 + 1);
cK = (int) (Math.random() * 499 + 1);
cL = (int) (Math.random() * 499 + 1);
startTime = System.nanoTime();
for (int i = 0; i < 1000; i++) {
if (cI < cJ) {
temp++;
}
if (cK < cL) {
temp++;
}
}
endTime = System.nanoTime();
totalTime = endTime - startTime;
AvgNType += totalTime;
temp = 0;
cI = (int) (Math.random() * 499 + 1);
cJ = (int) (Math.random() * 499 + 1);
cK = (int) (Math.random() * 499 + 1);
cL = (int) (Math.random() * 499 + 1);
long t1, t2;
startTime = System.nanoTime();
for (int i = 0; i < 1000; i++) {
if (cI < cJ) {
temp++;
}
}
endTime = System.nanoTime();
t1 = endTime - startTime;
AvgNN1Type += t1;
startTime = System.nanoTime();
for (int i = 0; i < 1000; i++) {
if (cK < cL) {
temp++;
}
}
endTime = System.nanoTime();
t2 = endTime - startTime;
AvgNN2Type += t2;
}
AvgNType /= 1000;
AvgNN1Type /= 1000;
AvgNN2Type /= 1000;
System.out.println("Test Number = " + tester);
Thread.sleep(500);
System.out.println("Avg time for Nested = " + AvgNType);
Thread.sleep(500);
System.out.println("Avg time for Non-Nested = " + AvgNN1Type + " + " + AvgNN2Type + " = " + (AvgNN1Type + AvgNN2Type));
avgRed += (AvgNN1Type + AvgNN2Type - AvgNType)*100/(AvgNN1Type + AvgNN2Type);
System.out.println("Reduction percentage = " + ((AvgNN1Type + AvgNN2Type - AvgNType)*100/(AvgNN1Type + AvgNN2Type)) + "%");
}
avgRed /= 25;
System.out.println("\n\n\nAverage Reduction = " + avgRed + "%");
}
}
Вот 2 типа результатов, которые я получил: -
Set 1 -
Test Number = 1
Avg time for Nested = 14207
Avg time for Non-Nested = 9799 + 10168 = 19967
Reduction percentage = 28%
Test Number = 2
Avg time for Nested = 8986
Avg time for Non-Nested = 4500 + 10066 = 14566
Reduction percentage = 38%
Test Number = 3
Avg time for Nested = 8933
Avg time for Non-Nested = 271 + 244 = 515
Reduction percentage = -1634%
Test Number = 4
Avg time for Nested = 8784
Avg time for Non-Nested = 192 + 184 = 376
Reduction percentage = -2236%
.
.
// Test Cases Skipped as all are similar
.
.
Test Number = 22
Avg time for Nested = 8666
Avg time for Non-Nested = 192 + 184 = 376
Reduction percentage = -2204%
Test Number = 23
Avg time for Nested = 8746
Avg time for Non-Nested = 194 + 187 = 381
Reduction percentage = -2195%
Test Number = 24
Avg time for Nested = 8680
Avg time for Non-Nested = 192 + 186 = 378
Reduction percentage = -2196%
Test Number = 25
Avg time for Nested = 8755
Avg time for Non-Nested = 193 + 187 = 380
Reduction percentage = -2203%
Average Reduction = -1989%
С другой стороны, у меня есть совершенно другой вид результата.
Set 2 -
Test Number = 1
Avg time for Nested = 10878
Avg time for Non-Nested = 7422 + 8753 = 16175
Reduction percentage = 32%
Test Number = 2
Avg time for Nested = 7991
Avg time for Non-Nested = 297 + 260 = 557
Reduction percentage = -1334%
Test Number = 3
Avg time for Nested = 8116
Avg time for Non-Nested = 5175 + 6135 = 11310
Reduction percentage = 28%
Test Number = 4
Avg time for Nested = 8660
Avg time for Non-Nested = 5092 + 9398 = 14490
Reduction percentage = 40%
Test Number = 5
Avg time for Nested = 185
Avg time for Non-Nested = 197 + 2723 = 2920
.
.
// Test Cases Skipped as all are similar (With reduction of all around 95%)
.
.
Test Number = 22
Avg time for Nested = 187
Avg time for Non-Nested = 204 + 2768 = 2972
Reduction percentage = 93%
Test Number = 23
Avg time for Nested = 203
Avg time for Non-Nested = 198 + 2772 = 2970
Reduction percentage = 93%
Test Number = 24
Avg time for Nested = 189
Avg time for Non-Nested = 196 + 2677 = 2873
Reduction percentage = 93%
Test Number = 25
Avg time for Nested = 192
Avg time for Non-Nested = 199 + 2763 = 2962
Reduction percentage = 93%
Average Reduction = 28%
Если бы я получал набор 1 только один раз каждые 100 попыток, я бы проигнорировал его, но я получил набор 1 примерно 25% раз я запускаю этот код.
что это за поведение?
Теоретически, я считаю, что набор 2 более точный, но это только в 75% случаев. Кроме того, в наборе 1 каждый раз ТОЛЬКО во 2-м или 3-м случае уменьшение слишком низкое (<-1500%). может кто-нибудь сказать мне, почему такие аномалии происходят? А что точнее? </p>
Я хочу реализовать лучшее в своем проекте, поэтому мне необходимо понять, что здесь происходит.