1

When trying to measure execution time I know that writing values to a file is much faster than printing values to the console (which can really slow the program down). But I haven't been able to find anything comparing the speed of writing values directly to a file vs. saving values in a growable data structure like a LinkedList during program execution and then writing all of the values to a file on program exit. It seems like saving values in a something like a LinkedList "might" be faster, but I don't know for sure.

I am trying to time an audio mixer loop running in its own thread, so it'll be self-timing. Therefore I'd like to use a technique for measuring and storing the times that makes as little impact on the overall run time as possible.

Also, I have looked at Java profilers like VisualVM, but haven't found a way to view execution times for individual passes through the loop.

Thanks!

Tekkerue
  • 1,497
  • 1
  • 11
  • 17
  • Who's going to read it? Why knock yourself out getting 10^6 individual function time intervals, saving them somewhere, and then averaging them down to 1 number? Why not just run it 10^6 times and divide the total by 10^6? There's other stuff going on, you say? Now we're getting somewhere. You want to know the *fraction* of time that routine is on the stack. – Mike Dunlavey Sep 25 '14 at 17:07
  • Hi Mike Dunlavey, thanks for responding. This is just for me, so I'll be the one reading it. I'm not going to be averaging them down to one number. I do actually want to see each loop's execution time. There are 'occasional' audio glitches that occur once in a while on a slower system and I want to see if my mixer is actually slowing down at that point. A single number won't tell me that. – Tekkerue Sep 25 '14 at 17:25
  • 1
    Any I/O is going to mess up your timing. OTOH, there are people who've asked why their graphic-update procedure runs fast but occasionally pauses. It's a hard thing to catch. The not-easy but effective method is to use a watchdog timer that interrupts whenever it hasn't been reset recently enough. When that happens, they can look to see why it happened. – Mike Dunlavey Sep 25 '14 at 17:31
  • The watchdog timer is an interesting idea, but how would you get the timing right in order to catch a glitch? Is it just essentially trial and error? Also, instead of performing an I/O operation, what about storing values in something like a LinkedList? Would that be an inexpensive operation that could be used for timing? Thanks again! – Tekkerue Sep 25 '14 at 18:33
  • 1
    Well, sure, a linked list or array has to be faster than I/O. To get the timing, you're back to trial-and-error or simple averaging. – Mike Dunlavey Sep 25 '14 at 18:56
  • Alright, thanks a lot Mike! – Tekkerue Sep 25 '14 at 19:05

1 Answers1

0

Writing to the file will interrupt your program -- not on every write but often enough to mess up your calculations. Writing to disk is done by the kernel and your entire program will wait for the IO. Saving the data to memory is better. However, watch out for ArrayList which may do a lot of extra work as the array grows. You are right to use a linked list.

You can use an in memory data base if the amount of data gets large.

Judd Rogers
  • 311
  • 3
  • 3
  • yeah store it in the RAM till testing done then write it out to perma if needed – tgkprog Sep 25 '14 at 18:44
  • Hi Judd Rogers, thanks for responding. That's essentially what I thought, but I wanted to be sure that was the case. And yeah, I know what you mean about the extra costs of using ArrayLists. The in memory database sounds like a great idea too, is there one you'd recommend for my use? Thanks again. – Tekkerue Sep 25 '14 at 18:48
  • -1 Sorry for downvoting, but even `LinkedList`s author stated that it was a mistake. While there are cases when it's superior to a non-presized `ArrayList`, they're too hard to find. A pre-sized `ArrayList` is always much better. See also [this](http://stackoverflow.com/q/322715/581205) and [this](http://stackoverflow.com/q/11564352/581205) questions. – maaartinus Sep 25 '14 at 19:13