Monday, April 15, 2019

God must be a programmer

Generally everybody tries to understand everything around them in their own way. Like for example, by seeing a chair, a chemist would think like 6 molecules carbon combined with 8 molecules of hydrogen to form that material. If a mathematician sees the same object he would think like 80 inches height and 55 inches width, 49 inches between two front legs... so in a similar way as I am a software engineer, I would relate everything as software around me.

One day I was thinking about god... what is god? Somebody said god is a superior then everybody. God is the creator of everything in this universe. God is the ruler of the universe and source of all moral authority. Then I was trying to understand by relating the entire world with a software which is coded by god in the following way.
  • The entire universe is actually a gaming/software environment created/defined by god/programmer. He must have created classes like Humans, Dogs, Cats, Fish etc… All the creatures including humans are different objects/instances of those classes. He must have followed object oriented principles to create such a huge software. 
  • Each object is driven by data and state. And having local storage capacity that's our mind. And of course the programmer can change the data wherever he wants which leads an object to do abnormal things. 
  • Creation and destruction of the objects is defined by god. Those are actually memory allocation and garbage collection 
  • The fully coded software is installed on a computer and we as objects treating it as a universe. 
  • The time actually is clock speed of the cpu on which the software is installed. Time runs at different speeds on different universe(1 hr on different planet is equal to 6 years on Earth) because the cpu clock speed might be different in two computers 
  • The internet connectivity from one machine to another machine is treated as black hole where an object goes to a black will actually find itself in another universe 
  • There is a possibility to install another software on the same computer.. we are treating object from other software as aliens. 
  • Personalities like Rama. Krishna are special objects with extra privileges to do more things then us.


After all these imagination, I can't resist myself from believing that god must be a programmer who created this environment and we are treating it as world.












Wednesday, August 3, 2016

System.currentTimeMillis() vs SystemClock.elapsedRealtime()

Are you trying to calculate the time duration between two events? better don't use `System.currentTimeMillis()`. use `SystemClock.elapsedRealtime()` instead.

System.currentTimeMillis() will give you the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (see setCurrentTimeMillis(long)), so the time may jump backwards or forwards unpredictably.

SystemClock.elapsedRealtime() return the time in milliseconds since the system was booted, and include deep sleep. This clock is guaranteed to be monotonic, and continues to tick even when the CPU is in power saving modes, so is the recommend basis for general purpose interval timing. Use elapsedRealtimeNanos() to get this `realTime` in nanoseconds

For example, you want to calculate a method execution time System.currentTimeMillis()

read current time at starting of method execution and store in a variable
long startTime = System.currentTimeMillis();
|
>> But what will happen if we change the time which is running the system/device. We have a fair chance of changing the device time by the user of your application right?
|
read current time at ending of method execution and store in a variable
long endTime = System.currentTimeMillis();

calculate the difference between startTime and endTime to get the answer 
long duration = endTime - startTime; 


Use SystemClock.elapsedRealtime() instead of System.currentTimeMillis() to not get effected by time setting changes. 

read current time at starting of method execution 
long startTime = SystemClock.elapsedRealtime();

read current time at ending of method executio
long endTime = SystemClock.elapsedRealtime();

calculate the difference
long duration = endTime - startTime;