Gin May
Gin May

Reputation: 43

do recopy a string to a new string have more performance impact that recopy an array?

I am new to Java and I do not know which is better
So I got a very strange homework question asked us to split the csv into 2d-array but their're 2 rules
1. you can't use ArrayList
2. You can't import other java class other than file.io

So this make it very challenging since we can't tell the array size yet. So when I readline, I decided to store it in a string first
after finished all the readline, only then I process the ","

So I wonder, do recopy a string to a string to add text have more performance impact or recopy previous array to new array is inefficient?
This is just an example what the csv may look like
1, 2, 3
4, 5, 6

stringRecopy &= reader.readLine()

vs

int[][] arrayRecopy = (copy previous array with new reader.readLine().split(",") here)

Upvotes: 0

Views: 50

Answers (1)

Stephen C
Stephen C

Reputation: 719239

I think you are asking the wrong question.

Unless the requirements for your homework assignment explicitly say that performance is the most important criteria, performance is a red herring.

In the real world, the important things about most programs1 are:

  1. They do what they are supposed to do; i.e. functionally complete, no bugs.
  2. They are maintainable; i.e. someone else can read and understand the code, and fix or modify it.
  3. They are implemented efficiently ... in terms of programmer time.
  4. They are fast enough for their intended purpose.

"As fast as possible" is rarely a criteria. Not least because achieving "as fast as possible" usually involves ignoring criteria 1 through 3 above.


So how does that apply here?

Well, what you are doing is worrying about the efficiency of a program that doesn't need to be efficient. Indeed, if I was marking this assignment2, I would pay a lot more attention to 1 and 2 than to performance. A program was written to be fast at the expense of maintainability would be down-marked!


So how would I implement it?

Well, the professional way would be to use ArrayList. But that is not an option3.

So that leaves two approaches:

  • Read (or process) the input twice. In the first pass work out how big the 2D array needs to be (by counting lines and the max number of commas in a line). Then create the array and do a second pass to populate the array.

  • Implement the code to grow the arrays as required.

Your criteria for deciding which approach to choose should be based on which one you think is easier >>for you<< to write, and for someone else to understand. Performance should be unimportant.


Finally, I don't think we can answer your actual question without seeing completed versions that implement both of your approaches. It very much depends on how you code them.

But once you have coded them and tested and debugged them, it should not be difficult to measure them. See How do I write a correct micro-benchmark in Java? for advice on how to do it in a way that will give you valid answers.


1 - There are notable exceptions to this.

2 - Note that I won't be marking your assignment, so you would be advised to think about what your teachers are actually looking for. If you are in doubt, you could ask them.

3 - This is not a criticism of the homework. I fully understand why they have made this restriction. This exercise is about learning to program, not (so-called) "best practice" programming in the real world.

Upvotes: 3

Related Questions