We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Hello,
I agree with you that is not going to dramatically improve the performance; but I think are goal is to always try to find the better solutions and even nanosecods matter.
Also is just one more line.
I ran some test and it actually helps the performace a little.
******This is the code ************
public class StringLength {
public static void usingLengthMethod(String s){
for(int i = 0; i< s.length();i++){
;
}
}
public static void whithoutLengthMethod(String s){
int limit = s.length();
for(int i = 0; i < limit;i++){
;
}
}
public static void main(String [] args){
long startTime,endTime;
String s ="Un lago en el cielo quiero ser suave para evitar tu dureza apago tu fuego enciende"
+"mi agua puede que no haya certezas Vamos despacio para el tiempo es arena en mis manos sé por tus marcas"
+"cuanto has amado más de lo que prometiste";
startTime = System.nanoTime();
whithoutLengthMethod(s);
endTime = System.nanoTime();
System.out.println("Declaring Size: " + (endTime - startTime) + " nano Seconds");
startTime = System.nanoTime();
usingLengthMethod(s);
endTime = System.nanoTime();
System.out.println("Using Length() method: " + (endTime - startTime) + " nano Seconds");
}
}
****RESULTS USING TERMINAL********
Test 1:
Declaring Size: 5480 nano Seconds
Using Length() method: 9196 nano Seconds
Test 2:
Declaring Size: 5021 nano Seconds
Using Length() method: 6803 nano Seconds
Test 3:
Declaring Size: 5144 nano Seconds
Using Length() method: 6887 nano Seconds
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tree: Huffman Decoding
You are viewing a single comment's thread. Return to all comments →
Hello, I agree with you that is not going to dramatically improve the performance; but I think are goal is to always try to find the better solutions and even nanosecods matter. Also is just one more line. I ran some test and it actually helps the performace a little.
******This is the code ************
****RESULTS USING TERMINAL********
Test 1:
Declaring Size: 5480 nano Seconds
Using Length() method: 9196 nano Seconds
Test 2:
Declaring Size: 5021 nano Seconds
Using Length() method: 6803 nano Seconds
Test 3:
Declaring Size: 5144 nano Seconds
Using Length() method: 6887 nano Seconds