Wednesday, 30 September 2015

Virtual Reality on Budget

Virtual Reality is a term that we are all familiar with, and would glad-fully be better off experiencing it ourselves. Until recently, VR has been an experience that's not very often associated with the word "Budget".

A couple days back, I purchased this VR headset for less than SGD25 from an online store, with shipping of course (may cost lesser elsewhere if you do more searching online).

VR Headset


This headset claims to turn your smartphone into a 3D cinema in an instant, and that you could enjoy virtual reality anytime and anywhere. This fits most smartphones with screen size 3.5 to 6 inches. Thus, compatible with my trusty Samsung Note 3 (tempted to remove my privacy filter on my phone to improve the visuals).

VR Headset (Open)

The Experience

Well, it was indeed an experience worth spending my 25 bucks on. The phone mounting is steady and holds up well to minor head movements while wearing it. With my Note 3 in there, the weight of the entire headset was acceptable and not too demanding (of course larger sized phones increases the weight of the whole thing).

Using it with my headphones (or earpiece) complements the immersive experience. You might want to use a phone with a decent screen resolution that still looks good while viewing up close, without looking too pixelised. For comparison, my Note 3 has a screen of 386 ppi (pixel density) and 1080 x 1920 pixels (Screen Resolution). Yes, most of the current latest phones would beat that, but hey.. its from 2013 anyways.

What about the apps? I would recommend some of these apps below, as I've gotten a fairly decent experience with them for the past couple of days.

"An expanding universe of Virtual Reality experiences. Viewable with or without Cardboard. Fully immerse yourself in 3D VR or full screen 360 featured stories, with dynamically changing directional binaural sound."
Comment: The downloadable original videos are of very good quality. The videos are quite generally quite large (200MB to more than 1GB). Ensure you are on wifi and not on cellular data, hopefully.

2. Mad Race VR
"Take part in the incredible race! Make dizzying jumps and unbelievable stunts! Feel yourself like a virtual pilot of buggy!"
Comment: Pair it with your headphones and enjoy the immersive yet crazy car race through obstacles. 
3. VR Cinema
"Imagine you are watching an exclusive motion picture. Feel complete penetration into the film, on a large screen, right at home, in your favourite cosy arm-chair."
Comment: One of my favourite. Download some video files onto your phone and watch it on the virtual cinema big screen, all in good virtual reality. While at it, remember to look around you and see the cinema surroundings and seats (you are alone in the cinema, so don't expect to see anyone else). 
One handy feature though, it auto corrects and centralises the big screen in front of you, so it has less drifting issues that might be found in other cinema apps.
4. YouTube! 
A simple search on YouTube for "side by side 3d" could get you some pretty awesome 3D experiences.
So there you go. If you are a VR fan too, do feel free to share other good apps for VR/3D. 

Tuesday, 29 September 2015

ConcurrentHashMap over Hashtable for cache lookup

A Legacy Class?

Hashtable is a legacy class before Java Collections Framewpork (JCF) was introduced. The synchronized methods of Hashtable obtain lock on the entire hashtable object during any retrieval or update operations. This can lead to increased response time under heavy load and impacts performance.

A better alternative?

Java SE 5 introduced ConcurrentHashMap, a map implementation which provides better concurrency and scalability as compared to Hashtable. Since the underlying data is stored in a series of buckets in a hashmap (note that a hashtable is also a hashmap implementation), it would be more efficient to lock only a bucket while accessing a map instead of obtaining lock on the entire map.

In other words, ConcurrentHashMap uses this mechanism which provides multiple bucket locks and reduces lock contention when multiple threads access a ConcurrentHashMap object concurrently. This improves performance over a Hashtable, especially in a highly concurrent system.


Multiple internal buckets of ConcurrentHashMap 

Simple Benchmark (in milliseconds)

Hashtable
Cache put (100,000 objects): 626.8
Cache get (100,000 objects): 0.45
Cache put (500,000 objects): 3,544.6
Cache get (500,000 objects): 0.47

ConcurrentHashMap
Cache put (100,000 objects): 501.2
Cache get (100,000 objects): 0.22
Cache put (500,000 objects): 3,159
Cache get (500,000 objects): 0.24

Summary

In summary, both classes are thread-safe. In terms of performance, ConcurrentHashMap also performs better than HashTable with improvements in object put/get in/from cache. Developers should consider replacing usage of Hashtable with ConcurrentHashMaps in projects to improve performance and efficiency.

Monday, 28 September 2015

Double alternatives for precise arithmetic

We sure have used double data type in our programming while performing simple or complex arithmetic calculations. Doubles tend to be good enough for most applications (not involving money of course), because if you are dealing with money, you need exact precision. 

Consider this:
double first = 2.00; 
double second = 1.10; 
System.out.println(first - second);
The result?
0.8999999999999999
The problem comes when double suffers from a "loss of precision" and becomes very noticeable when working with either very big numbers or very small numbers. You may be tempted to use print formatting to set the precision in terms of decimal placings, but that still do not solve the root issue.

Especially for monetary-critical applications, (i) you may want to consider using BigDecimal as it is an exact way of representing numbers. With BigDecimal this would not happen. (ii) use int or long to represent cents.

1. Using BigDecimal

Example
BigDecimal first = new BigDecimal("2.00"); 
BigDecimal second = new BigDecimal("1.10"); 
System.out.println(first.subtract(second));
Returns 
0.90
When using BigDecimal, note that it'll take up more memory, creates more garbage and is probably slower for most operations, as compared to using primitive types.


2. Using int or long

Example
System.out.println((200 - 110) + " cents");
Returns
90 cents 
In summary, avoid floating point calculations (e.g. float, double) when working with monetary or precision-critical applications. Consider greatly the use of int, long or BigDecimal instead.