Last month, I was building a web application that collected a lot of data via form submissions. I wrote a method to export attachments and form data by zipping them all together.
It worked for a smaller archives but as the records grew larger, I got a fatal error which said that I've exhausted the PHP's memory limit.
So, I found an easy way to increase the memory limit to 1024MB (1GB) like this:
<?phpini_set('memory_limit','1024M');?>
Before you write this stub in your script, make sure you have enough resources in your system or else, it'll go splat!
Did a little digging on whether it's useful to make use of pointers in your application
The topic about pointers isn't new as you're probably introduced to this concept in your first computer science class. I started learning Go and once again, I came across the concept of using Pointers and wanted to finally understand, why are they used and do we really need to use it?
Frankly, I don't really remember using pointers when I was developing web applications in PHP and JavaScript but it was quite a refresher to get back on track again.
Well, what is a pointer?
In it's basic form, a pointer is a variable that contains the memory address of another variable.
For example, you have a variable named x that contains some data, in this case, we'll store an integer:
packagemainimport"fmt"funcmain(){x:=10fmt.Println(x)// prints 10}
The code above prints the variable x by referring to it's name. We can do the same using pointers by referring to it's memory address via another variable that points to variable x.
To do this, we must use & operator to get the address and * to return the value stored in the address of variable x like this:
packagemainimport"fmt"funcmain(){x:=10p:=&xfmt.Println(x)// prints 10fmt.Println(*p)// also, prints 10}
Hmm, that was pretty straight forward.
So, why was it created?
In the modern era, most high-level programming languages are capable of handling memory allocation (like Java or C#), automatically.
But when C was developed, computers weren't as powerful as today but that also meant the programmers must create systems that made use of memory and speed, efficiently. Which is why some tasks do require it when programming low-level embedded systems or microcontrollers.
Why use them?
Whenever you pass a variable to a function, you're basically creating a copy of the variable. But if you're using pointers, you just have to pass the address of the variable and in most cases, a pointer is much smaller in size than the value being pointed at.
It allows you share data and it's quite appropriate when it comes to modifying the data being passed to it.
To some extent, it might seem efficient but there are some tradeoffs, when you're going to talk about optimization.
Do pointers really optimize your application?
I was thinking if pointers are used to optimize applications, I mean, you don't have to duplicate data and it saves memory, so why not?
Turns out that there are a few points that I came across:
Accessing values can use up memory though not so much but it can add up.
Data will be placed on top of heap stack which increases the memory overhead and can be cleaned up the garbage collector.
Most programmers tend to avoid using it in order to make their codebase less complex and reduce the increased work for the garbage collector.
Apart from that, there are some concerns when it comes to the security of the application implying that it could be unsafe when using pointers.
Takeaway
Pointers are useful but don't think of using it, blindly, by assuming that it might give you a boost in performance.
After reading a lot of articles about using Go as a programming language to write high-performing concurrent web applications and services yet maintain a clean codebase, I thought of starting to learn it to see what's it about and so far, it's been great!
This mini-tutorial will show you how to install Go on your system and write a simple "Hello, World" program.
Note: This tutorial will go through installing Go on Linux.
Install Go
Make sure you have the latest updates and upgrades on your Linux system before installing Go.
You need to download the binary file from their official package. Find the version that suits your OS and architecture.
I just started learning it today and at first, you might have the tendency to write code in Go the same way you write in any other language but beware, it doesn't work that way. It forces you to write good code including the way it formats the code using the gofmt tool.
Ever compressed your project directory but forgot to delete that useless file or folder and turns out the compresed file is larger than it's supposed to be?
Here's a quick solution you can try:
If you want to delete a file inside a .zip file, try this:
Finding the difference between two sets i.e. Set A and Set B is basically returning a new set that contains values of Set A that don't exist in Set B or vice-versa.
Here's an example:
vara=[1,2,3,4,5];varb=[1,1,2,3,4];
I wrote a shorter implementation using JavaScript's .filter() method:
Once you execute this method, you'll get the following as a result:
varc=array_diff(a,b);console.log(c)// returns [5]
BONUS: What does the .filter() method do?
This method returns a new array with elements that pass the conditions provided by a callback function.
If the conditions aren't passed, you'll receive an empty array.
In this article, (i => b.indexOf(i) === -1) is considered as the callback function in which the i refers to the index of the current element of array A and is then used as a parameter to check if the element doesn't exist in array B.
The neat thing about this method is that it doesn't mutate on the array that it's being called from.
This trick comes in handy whenever you wanted to reuse a table, perform data migrations or maybe even take a backup of the table before any of your experiments mess up your data.
Executing the following query will help you create a new table with the structure of the old table:
The lowerCount variable is taking the difference of the lengths between the original string and the string with lowercase letters only because the .replace() method replaced the pattern of uppercase letters [A-Z] with empty spaces. The upperCount variable does the opposite of what the lowerCount variable does.