Код: Выделить всё
unit test
share LazyLoop {
share main() {
arr := [0 to 1Qi]
// Test the condition directly
outln("Testing condition evaluation:")
outln("2 % 2 = " + (2 % 2))
outln("2 % 2 == 0 = " + (2 % 2 == 0))
outln("3 % 2 = " + (3 % 2))
outln("3 % 2 == 0 = " + (3 % 2 == 0))
// Also test with larger numbers
outln("24000 % 2 = " + (24K % 2))
outln("24000 % 2 == 0 = " + (24K % 2 == 0))
// Then run the loop
for i in [0 to 1Qi] {
if i % 2 == 0 {
arr[i] = "even"
} elif i % 2 == 1 {
arr[i] = "odd"
}
}
outln("\nResults:")
outln("arr[2] = " + arr[2])
outln("arr[3] = " + arr[3])
outln("arr[24000] = " + arr[24K])
outln("arr[24001] = " + arr[24001])
// NEW: Test the 2-statement pattern optimization
outln("\n=== Testing 2-statement pattern optimization ===")
arr2 := [0 to 1Qi]
for i in arr2 {
squared := i * i // temp = expression
arr2[i] = squared + 5 // arr[i] = expression_with_temp
}
outln("arr2[3] = " + arr2[3] + " (should be 3*3 + 5 = 14)")
outln("arr2[5] = " + arr2[5] + " (should be 5*5 + 5 = 30)")
outln("arr2[10] = " + arr2[10] + " (should be 10*10 + 5 = 105)")
// Test with different variable names
arr3 := [0 to 1Qi]
for i in arr3 {
temp := i * 2
arr3[i] = temp - 7
}
outln("\narr3[4] = " + arr3[4] + " (should be 4*2 - 7 = 1)")
outln("arr3[8] = " + arr3[8] + " (should be 8*2 - 7 = 9)")
// Test that it still works with conditional
outln("\n=== Testing conditional + 2-statement (not optimized yet) ===")
arr4 := [0 to 100]
for i in arr4 {
if i > 50 {
doubled := i * 2
arr4[i] = doubled + 1
} else {
arr4[i] = i
}
}
outln("arr4[30] = " + arr4[30] + " (should be 30)")
outln("arr4[60] = " + arr4[60] + " (should be 60*2 + 1 = 121)")
// Edge case: should NOT optimize (reassignment)
outln("\n=== Testing non-optimizable pattern ===")
arr5 := [0 to 100]
for i in arr5 {
x := i * 2
x = x + 1 // Reassignment - should break optimization
arr5[i] = x
}
outln("arr5[3] = " + arr5[3] + " (should be 3*2 + 1 = 7)")
outln("\n=== All tests completed! ===")
}
}
Код: Выделить всё
Enter file path or press Enter for default [/storage/emulated/0/JavaNIDE/Programming-Language/Coderive/executables/LazyLoop.cod]:
>
Using default file: /storage/emulated/0/JavaNIDE/Programming-Language/Coderive/executables/LazyLoop.cod
Testing condition evaluation:
2 % 2 = 0
2 % 2 == 0 = true
3 % 2 = 1
3 % 2 == 0 = false
24000 % 2 = 0.0
24000 % 2 == 0 = true
Results:
arr[2] = even
arr[3] = odd
arr[24000] = even
arr[24001] = odd
=== Testing 2-statement pattern optimization ===
arr2[3] = 14 (should be 3*3 + 5 = 14)
arr2[5] = 30 (should be 5*5 + 5 = 30)
arr2[10] = 105 (should be 10*10 + 5 = 105)
arr3[4] = 1 (should be 4*2 - 7 = 1)
arr3[8] = 9 (should be 8*2 - 7 = 9)
=== Testing conditional + 2-statement (not optimized yet) ===
arr4[30] = 30 (should be 30)
arr4[60] = 121 (should be 60*2 + 1 = 121)
=== Testing non-optimizable pattern ===
arr5[3] = 7 (should be 3*2 + 1 = 7)
=== All tests completed! ===
Ограниченный вопрос:
Какие конкретные, четко определенные шаблоны должны быть приоритетными для ленивой среды выполнения массивов, чтобы максимизировать оптимизацию O(1), и каковы четкие границы, где такая оптимизация становится невозможной?
В частности:
- Какие чистые шаблоны вычислений без побочных эффектов, выходящие за рамки простой арифметики (например, преобразование по модулю или линейное преобразование) реально получить O(1) с помощью ленивых вычислений?
· Полиномиальные выражения?
· Композиции чистых функций?
· Определенные шаблоны накопления (например, суммы арифметических рядов)? - Каковы однозначные, не подлежащие обсуждению условия нарушения, которые делают невозможным ленивую оптимизацию O(1)?
· Операции ввода-вывода
· Генерация случайных чисел
· Мутации нелокального состояния
· Зависимости от предыдущих итераций цикла
· Что еще окончательно ломает эту функцию?
Подробнее здесь: https://stackoverflow.com/questions/798 ... even-loops
Мобильная версия