https://kotlinlang.org logo
#feed
Title
# feed
c

Ch8n

01/20/2021, 7:34 AM
Big-Brain-Kotlin #7 | Equal Sides Share how you would solve this one... `Question`: For a given array, find Index N where the sum of integers to the left of N is equal to the sum of integers to the right of N, If there is no index that would make this return -1.
Copy code
Example:

for an array {1,2,3,4,3,2,1}

output would be 3 because at the 4th position of the array, 

the sum of left side of the index ({1,2,3}) and the sum of the right side of the index ({3,2,1}) both are equal to 6.
do check out 👇🏻 my solution https://chetangupta.net/bbk7/
a

altavir

01/20/2021, 7:50 AM
Performance measurement without JMH or at least naive large cycle are meaningless and probably completely wrong in this case. The variant with list involves boxing, but it should not increase time by an order and is completely optimized away on some of modern JVM implementations like GraalVM. Actual computation time for this task is in tens of nanoseconds, not miliseconds. You are measuring JVM warm-up time, not the performance. I think it would be better for people to learn thing first and than try to teach others, don't you think?
c

Ch8n

01/20/2021, 8:19 AM
Thanks that good insights, can you point me some resources where I build up on knowledge on this.
a

altavir

01/20/2021, 8:20 AM
I think this article could help: https://www.baeldung.com/java-microbenchmark-harness. https://github.com/Kotlin/kotlinx-benchmark has some facilities to do benchmarks in kotlin way.
❤️ 1
Also one needs to study JVM compilation mode switch, warmup and JIT stabilization in order to make any statements about performance. Since you seem to focus on Android, you should also remember that ART does not always work the same way as a desktop JVM. A lot of things to master if you start measuring and comparing things. Actually, I think for novices, the algorithm complexity is important, other things are just confusing.
c

Ch8n

01/20/2021, 8:34 AM
points noted .. thanks 🤓
s

seb

01/20/2021, 8:35 AM
Did you notice you have a typo in your cover image? 🙂
😁 1
c

Ch8n

01/20/2021, 8:59 AM
uploaded old banner, fixed it now thanks @seb
👍 1