1

In terms of performance and memory, which would be the most efficient?

Right now, I have a List<ArrayList<String>> that holds almost 200,000 records, generated by a separate business logic. I am trying to print it in a table format in a HTML page using Thymeleaf. Currently, I am using nested th:each loop to print the data with th:text. As expected, this takes a long time.

I looked into this and found that th:utext can be used to print html content in Thymeleaf.

My business logic to generate these 200,000 records, currently gives a List<ArrayList<String>> object, but I can modify it to a List<String> object by making changes.

My question, however, is that, will that be more memory-efficient. It is very much possible that it will be more performance efficient, but my main concern is memory-efficiency. Would a List<ArrayList<String>> take up the same amount of memory as a List<String> containing the same data ? (By same data, I mean, the content is the same - instead of an ArrayList<String>, it's going to contain a single string with HTML elements in between). Or would it take up more memory?

I am relatively new to understanding space complexity of data structures; hence the query.

3
  • 1
    lol trying to create a html table with 200K data will never be efficient :D. probably you may want to create a list of paging instead. ArrayList of 200K is not much, I think your problem is having a string that contains 200K lines Commented Aug 4, 2017 at 13:42
  • @nafas has a good point. Using your current structure, it would be easy to implement a pagination feature. If, however, you are stuck on posting these 200k records, there is no performance efficiency increase or decrease from changing it to a List<String>. There may be a very slight memory decrease from the change. Commented Aug 4, 2017 at 13:44
  • As @Kylon said, it won't make a big difference. But: within the cells of 200K records, I'd expect many duplicate entries, which don't need to have an individual String instance, each. Trying to share the common String instances (maybe with String.intern()) might reduce your memory footprint. With the List<String> version, the probability of common complete lines is surely lower. But maybe all this doesn't matter if Thymeleaf first creates a StringBuilder with the full page instead of outputting the elements on-the-fly (I don't know Thymeleaf) - then you're lost. Commented Aug 4, 2017 at 14:04

1 Answer 1

1

Leaving aside the oddness ;) of rendering a HTML table with 200k records, the most commmon answer to a question of this form ...

In terms of performance and memory, which would be the most efficient?

... is: measure it!

Your suggested approach (replace a List<ArrayList<String>> with a List<String>) implies that you hope for the following:

  • The List<String> has a smaller footprint than the List<ArrayList<String>>
  • Thymeleaf has less work to do in handling the List<String>
  • Any benefits accruing to use of the List<String> are not outweighed by the additional cost of turning the List<ArrayList<String>> ... generated by a separate business logic into a List<String> on the server side

I think the only way you will be sure of this is to compare some metrics produced by both approaches. For example:

First order measurements:

  • Measure the time taken to create the response server side
  • Measure the time taken to render the response in the browser

Second order measurements:

  • Measure the memory and CPU usage server side while creating and emitting the response
  • Measure the memory and CPU usage client side while rendering the response

I suspect that implementing the suggested changes might be quite simple and certainly the first order measurements are easily gathered. So, rather than trying to assess the impact from first principles, just do it and see whether it helps you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.