Depends upon the optimizer - both execute 100,000 assignments in the end. If the optimizer is good, it will detect that you are assigning x from y without any computation, so it would optimize out the loops and just assign y to x once.
Obviously Code 2 will run fast, the reason being is inner for will run for 100,000 iterations in both codes, its the outer for loop which runs 1000 times in code 1 and 100 times in code 2 therefore code 2 is 10 times faster
Ah! The black boxes of compilers and optimizers! In theory, the inner loop is executed 100K times, in either case. How the compiler performs loop optimizations is the biggest issue here. In theory, the one where the outer loop is only executed 100 times should be faster (fewer initializations of the inner loop), but as nitin1 indicated, the first one was actually faster. I stand by my contention that it was a compiler optimization side-effect, and the only way to tell if one is faster than the other is to disable all optimizations. I suspect that they should, in such a case, run identically! Or close enough over a number of runs to be such... :-)
Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...