Tag Archives: Code

Caching data can save a computer from doing unnecessary processing work and can be a simple way to speed up your programs. There are two types of caching you could use, in-memory(RAM) and out-of-memory(Physical Storage). Storing data onto a hard drive is generally the way to go if you’re working with larger data. The downsides of storing information within a system’s RAM is that won’t always know how much is available at run-time or how it will affect the system overall. The benefits on the other hand is that it’s a fast and simple way to allocate memory for a program when you need it.

I’m not going into too much detail on how to set up a physical cache, this post was created because I wanted to run some speed tests on a simple Java program I wrote using a mapping data structure. I made two different types of Fibonacci sequence generators. I’ll mention a few other designs considerations if you want to take this a step further but this was quick program I spent about 45 minutes on. I wrote these scripts pretty quickly and didn’t completely iron out a few details I found annoying so bare with me. The first version of a Fibonacci sequence generator I made was a basic recursive version that might look familiar.

There wasn’t a lot of code I had to write, findFibonacci() will generate the sequence up to a given range. As I mention annoying details, I didn’t store any of the data generated to print out the data. I consider about storing every found Fibonacci number into a map so you could see when that number was generated but that would quickly grow into a large data set that wouldn’t print neatly at the console. Feel free to store or print each generated value if you want to see when a number is generated. You can also break down the logic into a process tree by hand if you’re filling up to it. Word of warning, if you’re not familiar with the Fibonacci sequence, it’s a repeating pattern found in natural and grows exponentially. Also not a reason not to bread rabbits. The next version I made was to take this code to store any new Fibonacci numbers into an map and retrieve them when necessary.

If you hadn’t notice I used an abstract class called FibonacciSequencer to build these two Java classes. The objects Recursion() and MemCache() only contain the Fibonacci algorithm but they both use the testing code within the FibonacciSequencer() object. There are some unused code here but I only executed the function, trials() outside this object. My trials() method takes two arguments, one for how many trials to run and the second is the range of the Fibonacci sequence. I was pretty lazy so I print out how well the sequence generated did I made throw the output from the trials() function into the printTrials(). The next part after this class if my test harness if you want to see how I used these two functions.

Well, averaging the results from ten trials and finding up to the sixth index of a Fibonacci sequence generated these results.

Memory Cache Test

10 trials where executed.
Average time of completion was, 87 microseconds.
Time of completion as nanoseconds, 87637.
Time of completion as milliseconds, 0.08763699999999999.
1, 1, 2, 3, 5, 8.

Recursion Test

10 trials where executed.
Average time of completion was, 4 microseconds.
Time of completion as nanoseconds, 4274.
Time of completion as milliseconds, 0.004274.
1, 1, 2, 3, 5, 8.

Ok, the recursion function won out because I used a small Fibonacci sequence. There was some small overhead in the hash map version that slowed down program but lets try running a slightly larger sequence. The next tests found a sequence of fifty values. Let’s see how long it takes only for the hash map.

Memory Cache Test

10 trials where executed.
Average time of completion was, 92 microseconds.
Time of completion as nanoseconds, 92246.
Time of completion as milliseconds, 0.092246.

Minus that little bug at the end of the sequence the program average around 95,281 milliseconds to complete, not microseconds. As you can see the recursion method breaks down quickly once you start increasing the scale. This is pretty much end of how far I took this simple script but here are few ideas you could try implementing to improve the performance.

Store the data into a file, simple and highly efficient. This way you’re not dependent on the scale of the Fibonacci sequence and you can still incorporate in-memory caching to tackle the problem in manageable blocks. You also should use some kind of compression on the file to keep it manageable, if you don’t want to code you’re own compression algorithm you can using an existing algorithm like gZip to do the heavy lifting. Also try working with binary files rather than text string files.

Here is my test harness if you want to try running my scripts. I have some time this weekend to work on another quick and simple programming exercise. Right now I’m leaning towards writing a Monty Carlo program in Python, but it would feel a little weird doing it in any other language besides lisp. Side note, why can’t languages support an non-crippled lambda expression. ;(

The class is taught by Erik Demaine, Ronald Rivest, and Srinivas Devadas. Ronald was also one of the co-authors the book Introduction to Algorithms, a good book worth checking out.

I figure good place to start would be to write up a few sorting algorithms I’m already familiar with and see if I optimize them while going through the lectures. Here are is my non-optimized Java code.

To make my life a little easier, I made an abstract class called Algorithm to hide some ugly code and make sure my sorting algorithm class just have a sort() method.

*Note: I’ve did this in a VM and I don’t have a flash drive with me at the moment. I just rewrote these objects but they may contain some typos. Later I copy and paste them here along with a few others I made.

Now that’s out of the way lets get to the fun part. I wrote most of these algorithms with help from WikiBook’s Sorting Algorithm page. If you want any more additional information about these algorithms, check out Wikipedia, cool things can be found there.

Sadly, this version is a tad bit too slow for my tastes so I’ve made another heap sort using the in-place method. You are about too see why I liked the priority queue version. I didn’t bother cleaning up the source code much. The code one big iteration loop with nested if statements that work off the branches.

I’ve made this compression program in Java after watching this lecture from Harvard’s OpenCourseWare class, Information and Entropy. My code is still incomplete but it appears to be working as it should. I also took out some of my code and only included the example to compress a single string. I chose to solve this problem with iteration rather than recursion. As it is now it’s kinda of a mess of nested while loops. Later I’m going to try to encapsulate that code into an iterator and add some extra logic to build the dictionary faster.

I’m just about done reworking the recursive function to plot the mid points of a triangle and create nested triangles within it. I still have to tweak it a bit because I want the middle triangle to be the color of the canvas. Not entirely sure how to do that yet but I’ll update this post when I figure it out.

There are a few things I want to quickly fix on my Sierpinski triangle JavaScript. I reworked how I drew the triangle onto the canvas this morning and I like how it turned out. Next I’m going to attempt to dynamically plot an equilateral triangle onto the canvas.