I was writing a program that uses some file processing using the file class and a Buffered Input and Output Streams and found that when I used the following code
for(long j = 0; j < f2.length(); j++) {
out.write(in.read());
}
was highly inefficient, taking about 15251 milliseconds. However if I saved f2.length() to a variable first
long y = f2.length();
for(long j = 0; j < y; j++) {
out.write(in.read());
}
it only takes 74 milliseconds. Why is the second block so much more efficient?
Is it possible that saving f2.length() in a variable y will only compute length() once, and in a for loop it will compute for every iteration, multiplying f2.length() computations n times for n number of iterations.
I am curious too. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.