ソース
testArray7が実は100入れたらバグってるんだが止まらなかったなぁ・・・。
class test {
public String testArray100(int x) {
String re[] = { "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C",
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C",
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A" };
return re[x];
}
public String testArray7(int x) {
String re[] = { "C", "", "B", "", "", "", "A" };
int a = (x / 80) << 2 | (x / 50) << 1;
return re[a];
}
public String testArray3(int x) {
String re[] = { "C", "B", "A" };
int a = x >= 80 ? 2 : x >= 50 ? 1 : 0;
return re[a];
}
public String ifSingle(int x) {
if (x >= 80)
return "A";
if (x >= 50)
return "B";
return "C";
}
public String elseIfSingle(int x) {
if (x >= 80)
return "A";
else if (x >= 50)
return "B";
else
return "C";
}
public String ifBlock(int x) {
if (x >= 80) {
return "A";
}
if (x >= 50) {
return "B";
}
return "C";
}
public String elseIfBlockA(int x) {
if (x >= 80) {
return "A";
} else {
if (x >= 50) {
return "B";
} else {
return "C";
}
}
}
public String elseIfBlockB(int x) {
if (x >= 80) {
return "A";
} else if (x >= 50) {
return "B";
} else {
return "C";
}
}
わからなすぎて分からない呼び出し部分
import java.util.Random;
import java.lang.reflect.Method;
public class App {
public static void main(String[] args) throws Exception {
String fName[] = { "testArray100", "testArray7", "testArray3", "ifSingle", "elseIfSingle", "ifBlock",
"elseIfBlockA", "elseIfBlockB" };
String strClass = "test";
Class<?> c = Class.forName(strClass);
Object obj = c.getDeclaredConstructor().newInstance();
String[] str=new String[8];
for (int i = 0; i < 8; i++) {
Method m2 = c.getMethod(fName[i], int.class);
long startTime = System.currentTimeMillis();
for (int j = 0; j < 1000; j++) {
Random random = new Random();
int x = random.nextInt(101);
System.out.print(x + "は" + m2.invoke(obj, x) + "です");
}
str[i] = fName[i] + "の処理時間:" + (System.currentTimeMillis() - startTime) + " ms";
}
System.out.println("");
for (int i = 0; i < 8; i++) {
System.out.println(str[i]);
}
}
}
一番スッキリかけた感じ
from ast import Num
import random
from time import time
def testProcess_array100(x):
re = ["not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB",
"not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB",
"not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB",
"not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB",
"not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB", "not AB",
"not AB", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A"]
return re[x]
def testProcess_array7(x):
re = ["not AB", "", "", "B", "", "", "A"]
a = ((x//80) << 2) | ((x//50) << 1)
return re[a]
def testProcess_array3(x):
re = ["not AB", "B", "A"]
a = 2 if x >= 80 else 1 if x >= 50 else 0
return re[a]
def testProcess_ifSingleA(x):
re = ["not AB", "B", "A"]
if x >= 80:
a = 2
elif x >= 50:
a = 1
else:
a = 0
return re[a]
def testProcess_ifSingleB(x):
re = ["not AB", "B", "A"]
if x >= 80:
a = 2
else:
if x >= 50:
a = 1
else:
a = 0
return re[a]
proc_time = []
funcs = {"testProcess_array100", "testProcess_array7",
"testProcess_array3", "testProcess_ifSingleA", "testProcess_ifSingleB"}
for index,func in enumerate(funcs):
start1 = time()
for i in range(1000):
x = random.randint(0, 100)
re = locals()[func](x)
print(str(x) + "は" + re + "だよ")
proc_time.insert(index, func + ": " + str(time() - start1))
print(proc_time)
runでどう頑張ってもfunction呼べなくて断念。
Option Explicit
Sub main()
Dim i As Integer
Dim r As Integer: r = 1
Dim startTime As Double
Dim j As Integer
For j = 0 To 5
Sheet3.Cells(r, 4) = "array100"
startTime = Timer
For i = 0 To 1000
Debug.Print (array100(Int((100 - 0 + 1) * Rnd + 0)))
Next
Sheet3.Cells(r, 5) = Timer - startTime
r = r + 1
Sheet3.Cells(r, 4) = "array7"
startTime = Timer
For i = 0 To 1000
Debug.Print (array7(Int((100 - 0 + 1) * Rnd + 0)))
Next
Sheet3.Cells(r, 5) = Timer - startTime
r = r + 1
Sheet3.Cells(r, 4) = "array3"
startTime = Timer
For i = 0 To 1000
Debug.Print (array3(Int((100 - 0 + 1) * Rnd + 0)))
Next
Sheet3.Cells(r, 5) = Timer - startTime
r = r + 1
Sheet3.Cells(r, 4) = "testIf"
startTime = Timer
For i = 0 To 1000
Debug.Print (testIf(Int((100 - 0 + 1) * Rnd + 0)))
Next
Sheet3.Cells(r, 5) = Timer - startTime
r = r + 1
Sheet3.Cells(r, 4) = "testIfBlock"
startTime = Timer
For i = 0 To 1000
Debug.Print (testIfBlock(Int((100 - 0 + 1) * Rnd + 0)))
Next
Sheet3.Cells(r, 5) = Timer - startTime
r = r + 1
Sheet3.Cells(r, 4) = "testElseIfBlock"
startTime = Timer
For i = 0 To 1000
Debug.Print (testElseIfBlock(Int((100 - 0 + 1) * Rnd + 0)))
Next
Sheet3.Cells(r, 5) = Timer - startTime
r = r + 1
Next
End Sub
Function array100(x As Integer) As String
Dim ret As String: ret = Sheet3.Cells(x + 1, 1)
array100 = ret
End Function
Function array7(x As Integer) As String
Dim re(8) As String
re(0) = "C"
re(2) = "B"
re(6) = "A"
re(8) = "A"
Dim a As Integer
a = ((x \ 80) * (2 ^ 2)) + ((x \ 50) * (2 ^ 1))
array7 = re(a)
End Function
Function array3(x As Integer) As String
Dim re(3) As String
re(0) = "C"
re(1) = "B"
re(2) = "A"
Dim a As Integer
a = IIf(x >= 80, 2, IIf(x >= 50, 1, 0))
array3 = re(a)
End Function
Function testIf(x As Integer) As String
Dim re As String
If x >= 80 Then re = "A" Else If x >= 50 Then re = "B" Else re = "C"
testIf = re
End Function
Function testIfBlock(x As Integer) As String
Dim re As String
If x >= 80 Then
re = "A"
Else
If x >= 50 Then
re = "B"
Else
re = "C"
End If
End If
testIfBlock = re
End Function
Function testElseIfBlock(x As Integer) As String
Dim re As String
If x >= 80 Then
re = "A"
ElseIf x >= 50 Then
re = "B"
Else
re = "C"
End If
testElseIfBlock = re
End Function