![]() if you have to write high-efficient algorithms, for most of us it’s just an unnecessary concern. To be honest, it’s only a micro-optimisation on a bigger scale. I’m not sure, whether this experiment will change the way how I write the code. It has specific use cases and meaning so don’t hesitate to use this function if it fills your needs. It doesn’t mean, that you should not use the array_reduce function. For smaller datasets, differences are not noticeable. Fortunately, the test was deliberately exaggerated to shows differences between each approach. I personally use the array_reduce approach quite often. It shows, that we should always test and validate our idea. The solution that seemed to be the best turned out, that it was the least efficient. My first impression was something like a mixture of confusion with misunderstanding. The chart presenting average duration of each approach. First, please take a look at the final outcomes.ĭifferences are more visible on a simple bar chart. I have to admit, that results were really surprising to me. It allows me to easily calculate the average time and plot a simple chart. The test generates an output which I copied and paste as a CSV to the Excel. I rented a droplet 2GB RAM / 2 CPU from DigitalOcean which ready-to-use LAMP Stack on board and I run the php -v In short – I execute each function 5000 times and I measure the duration of the execution.įirst I tried to run this script on my local environment, but I came to the conclusion, that the bettser option is to use a completely different server. ![]() Although it’s not so complicated, it’s enough to see which implementation is better, relatively to each other. I prepared a simple test file, you can view it on this GitHub Gist. That’s why I test which implementation work faster. Otherwise, I collect some ideas and I want to validate them in such way. Personally, I don’t like do and read benchmarks for things that usually doesn’t matter. The loop logic is hidden within the array_reduce function so only care about the correct assignment. The only thing we should do is to create a function which will assign the transformed value to specific key in the array (similar as on 1st approach). We actually have an array of Country objects, which definition is presented below:įunction prepareCountriesForCombo ( array $countries ) : array ) The user should see the full country name instead. In our database, we store information about the country in the ISO-3361-alpha-1 format. During the registration process, the user can select its own country. In our application, we have users from different parts of the world. Let’s focus on how we can build the associative array from a vector. ![]() ![]() Instead of thinking about the whys, image the situation when we could use a hashmap. Named keys are also more meaningful than numeric indexes. There are some reasons why we can use associative arrays instead of simple vectors, e.g. They provide easy access to any element of the array as long as we know the key corresponding with that value. ![]() We can use nested arrays but it doesn’t cause that the specific item within the array will be easy to identify. But there are a lot of situation when we need to carry extra information with our data. When we talk about arrays we usually mean the vector of something – primitives, objects or even arrays. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |