waitinfuture commented on PR #2086:
URL:
https://github.com/apache/incubator-celeborn/pull/2086#issuecomment-1805300355
I found the following code is even several times faster: 1187677ns vs.
513241ns vs. 32007ns vs. 9436ns
```
val host = generateRandomIPv4Address
val rpcPort: Integer = Random.nextInt(65535)
val pushPort: Integer = Random.nextInt(65535)
val fetchPort: Integer = Random.nextInt(65535)
val replicatePort: Integer = Random.nextInt(65535)
var startTime = System.nanoTime()
val state = Seq(host, rpcPort, pushPort, fetchPort, replicatePort)
// origin
val originHash = state.map(_.hashCode()).foldLeft(1)((a, b) => 31 * a +
b)
var endTime = System.nanoTime()
println(endTime - startTime)
// for
startTime = System.nanoTime()
var forHash = 1
for (i <- state) {
forHash = 31 * forHash + i.hashCode()
}
endTime = System.nanoTime()
println(endTime - startTime)
// while
startTime = System.nanoTime()
var whileHash = 1
var i = 0
while (i < state.size) {
whileHash = 31 * whileHash + state(i).hashCode()
i = i + 1
}
endTime = System.nanoTime()
println(endTime - startTime)
startTime = System.nanoTime()
val objectHash = Objects.hash(host, rpcPort, pushPort, fetchPort,
replicatePort)
endTime = System.nanoTime()
println(endTime - startTime)
```
But we need to explicitly set type of `rpcPort, pushPort, fetchPort,
replicatePort` to `Integer`, or use `Integer.valueOf(rpcPort` int the hash
method. In addition, the behavior changes a bit that `Objects.hash` default use
0. IMO it's OK because it only affects the process.
--
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]