LuciferYang commented on pull request #35350:
URL: https://github.com/apache/spark/pull/35350#issuecomment-1027925630
The following two methods are simulated and tested with nested objects:
I do a test for the following 2 methods:
```java
// use loop
public static boolean isNumber1(Object o) {
while (true) {
if (o instanceof NestedObject) {
o = ((NestedObject) o).innerValue();
continue;
}
return o instanceof Number;
}
}
// use recursion
public static boolean isNumber2(Object o) {
if(o instanceof NestedObject) {
return isNumber2(((NestedObject) o).innerValue());
}
return o instanceof Number;
}
```
The test code as follows:
``` scala
object LoopAndRecursionBenchmark extends BenchmarkBase {
override def runBenchmarkSuite(mainArgs: Array[String]): Unit = {
val valuesPerIteration = 10000;
doTest(valuesPerIteration, create1LayerNestedObject(), 1)
doTest(valuesPerIteration, create3LayerNestedObject(), 3)
doTest(valuesPerIteration, create5LayerNestedObject(), 5)
}
def doTest(
valuesPerIteration: Int, obj: NestedObject, layer: Int): Unit = {
val benchmark = new Benchmark(
s"Test loop and Recursion $layer",
valuesPerIteration,
output = output)
benchmark.addCase("Use loop") { _: Int =>
for (_ <- 0L until valuesPerIteration) {
TestLoopUtils.isNumber1(obj)
}
}
benchmark.addCase("Use Recursion") { _: Int =>
for (_ <- 0L until valuesPerIteration) {
TestLoopUtils.isNumber2(obj)
}
}
benchmark.run()
}
def create1LayerNestedObject(): NestedObject = {
new NestedObject(1)
}
def create3LayerNestedObject(): NestedObject = {
new NestedObject(new NestedObject(new NestedObject(1)))
}
def create5LayerNestedObject(): NestedObject = {
new NestedObject(new NestedObject(create3LayerNestedObject()))
}
}
class NestedObject(inner: Any) {
def innerValue(): Any = inner
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]