I would like to know the time complexity of the following code snippet,
FileReader fr = new FileReader("myfile.txt");
BufferedReader br = new BufferedReader(fr);
for (long i = 0; i < n-1; i++ ) {
br.readLine();
}
System.out.println("Line content:" + br.readLine());
br.close();
fr.close();
Edit: I would like to say, n = a constant number, e.g. 100000
The complexity is O(n) but that doesn’t tell you much because you don’t know how much time each
readLine()needs.Calculating the complexity doesn’t make much sense when individual operations have a very variable runtime behavior.
In this case, the loop is very cheap and will not contribute much to the runtime of the whole program. The loading from disk, on the other hand, will contribute very much to the runtime but it’s hard to say without statistical information about the average number of lines per file and the average length of a line.