Magic SysRq

The fundamental reason behind a system hang or freeze is typically resource exhaustion; resources necessary for some part of the system to run are not available, due to being in use by other processes or simply insufficient. It’s very common problem to a programmer’s life. Even a database query responding with large data set can eat up so much memory that a system can freeze or an infinite loop is capable enough to stall the system anytime. Continue reading

Avoid if/ else within loop

Yes, this post is about the most basic concept that using if/ else should be avoided within loop as much as possible. Optimizing code is always important and when it’s about performance then the basic efficiency rules should be followed at first. Practically there are many situations when a loop iterates over large data set e.g. 1m; there may be different required computation in the same loop- but if possible the loop should maintain branch prediction; avoid unnecessary if/ else.

Following simple rules lead to performance advantage certainly. Branch prediction is a simple concept to optimize a loop-
Continue reading

What is deep copy and shallow copy? How PHP uses them?

I got this question in an interview. Was aware of this conception and explained properly. But I think this topic is worth to explain, so that’s why this post is-

Shallow copy– In the process of shallow copying A, B will copy all of A’s field values. If the field value is a memory address it copies the memory address, and if the field value is a primitive type it copies the value of the primitive type. In layman’s term shallow copy duplicates as minimum as possible. But the disadvantage is if you modify the memory address that one of B’s fields point to, you are also modifying what A’s fields point to.

Deep copy– In this process the data is actually copied completely. The advantage is that A and B do not depend on each other but this process is relatively slower and more expensive.
Continue reading