1

I have a list with each line of the text below:

STARTTIMING: main
| STARTTIMING: loop
| | STARTTIMING: doSomeStuff()
| | STOPTIMING: doSomeStuff() 1ms
| | STARTTIMING: doSomeStuff()
| | STOPTIMING: doSomeStuff() 1ms
| STOPTIMING: loop 5ms
STOPTIMING: main 5ms

I know how to format it by using "brute force", but once it is an application for measuring performance, what is the fast way to print the strings in the format showed above (indent properly, and inserting vertical bars) in Java?

irios
  • 165
  • 1
  • 11
  • 1
    What does your current code look like? are you using `StringBuilder` or just regular `String`? – Fermat's Little Student Mar 29 '15 at 03:05
  • @Will I'm setting in a array the strings that I get from my measurements. I have like `STARTTIMING: main`, `STARTTIMING: loop`, `STOPTIMING: doSomeStuff() 1ms` in each position of the array. My algorithm have to decide when ident, and how many idents. – irios Mar 29 '15 at 17:52

1 Answers1

1

Modern JVM's optimize string concatenation for you. I wouldn't bother trying to outsmart it unless you're doing something really painful. I can't tell if you are since you didn't post your actual code.

Here's little bit of help

indentString = new String(new char[indentCount]).replace("\0", "|");

that I stole from here

Also, if you mean to say you're worried about the time concatenation takes because it's part of the time measuring code then for Peat's sake measure the time difference before doing any concatenating, or anything else.

Community
  • 1
  • 1
candied_orange
  • 7,036
  • 2
  • 28
  • 62