Комментарии:
sir kindly can you share the link to the video in which you teach about cursor() loading, I think right after this but unfortunately, I didn't find it in your list.
ОтветитьIs there any way to measure the memory usage of a collection? I have a killed job issue when operating huge data through collection...
ОтветитьI think you're comparing oranges to apples, please use other method like countby or some other method, why we need a group by here
ОтветитьI like this video.
Once we hade perfommance issue with data-transformation, and my Lead Dev, ask me to write DB query; instead of using Eloquent Collection.
For 100.000 rows On DB, the differences was Huge.
It takes more time for devs, but for huge amount of data It is always better to write Optimized code on the begining, instant of rewriting when the dB get bigger.
I think the main power of Collection is the sugar they add. Yes, they could slow your code to operate. But coding is not only execute the code. It's also about writing and maintaining your code. So, the true power of Collection is that you code faster.
ОтветитьBecause the groupby method is an aggregation that's why its is slow. You will notice it if you are working on TSQL with complex queries
ОтветитьI am also interested to know what is easier to write and to read. Maybe there are situations where the execution time is not that important, but the time to develop the code is. First make things work, later make it faster when needed. That being said, I really like this topic because I feel it is important to make informed decisions on stuff like this. Thanks for the video!
ОтветитьI've not done benchmarks, but I do recognise the time saved debugging and understanding code that I've come back to after a significant time period as a good reason to use collections, as opposed to loops as in the first example.
ОтветитьI'd be interested in the time an SQL query would take to perform this operation.
Ответитьso the conclusion is array 2x fast than collection?
ОтветитьAn excellent example for collections and arrays 👍
ОтветитьCollection readability is less intuitive.
ОтветитьI haven't read the code of Collection but most of them should be wrappers for php native methods so you shouldn't see too much performance difference.
ОтветитьIn this particular case, I believe you could make better use of Lazy Collection (included in the framework since laravel 6). One of the examples from the original PR was precisely about dealing with these kinds of large CSV files.
Some methods are not available to LazyCollections however, like mapWithKeys, but I think you can get the exact same result by changing with `->map->count()`.
use Illuminate\Support\LazyCollection;
LazyCollection::make(
function () {
$handle = fopen(public_path('users.csv', 'r'));
while( ($line = fgets($handle)) !== false ) {
yield $line;
}
})
->map(...)
->filter(...)
->groupBy(...)
->map->count()
->sortDesc();
Try it out!
Great comparison thanks, I'd like to know, what javascript framework is more popular with Laravel, and what is more efficient ????
Ответитьcollect($users)->countBy(function ($user) {
return Str::of($user[1])->before(' ')->toString(); //strstr($user[1],' ', true);
});
->except(['Ben', 'Vesta','Lisa'])
->sortDesc();
Supplier for the best Laravel tips and tutorials!
ОтветитьHello Sir,
can you make a video on web 3.0 and where does php and laravel stands in it and how to create projects using these. or any other info related web 3.0 and php/laravel.
Thanks
I hope we once get a typed collection data structure
ОтветитьDo it with a raw SQL query and try again.
ОтветитьComparing native implementation against a wrapper of native implementation? Native would always be faster.
ОтветитьGreat explanation! Great comparison! Great experience! Thank you!
ОтветитьShould of ran it on millions of rows imo
ОтветитьIf you want to have a single run through using collections then I think using lazy collection can achieve that.
ОтветитьWould be interesting to compare this with `lazy` collections
ОтветитьGreat video. Could you use countBy collection method rather than groupBy and mapWithKeys ?
This is specific to this use case but here the groupBy is doing not necessary calculation.
Will the use of Lazy Collections in this case save time or is it just reducing the memory the application uses??
ОтветитьI think it was Steve McConnell that said that it is important to "make the code work, then make it fast". Until it works and you test and measure it you don't know which parts are making your application slow. This is an example of what he was saying.
The more experienced you are the more you know what code constructs are faster than others but the real test is to measure it.
But only when the application works.
I wish we analyzed time complexities of each solution before running the test. Anyway, if you want effectively use Laravel collections, it is a good idea to know time complexities of the methods.
ОтветитьIMO, developer time is more important than the run-time. Since we have very fast connections and very strong hardwares, slighty slower code won't effect user experiance negatively. If a table becomes huge and starts slowing down the whole system, it will always be an option to recreate its queries and codes to improve the performance. So my motto "use collections until you shouldn't." Spending your time today for a future that might never come is not a good idea.
ОтветитьI don't think it's a fair comparison.
In the first example, only one loop is made, that is: 100 000 lines.
In the second, there are 2 maps, a filter and a group, totaling 400 000 lines.
As stated in the video, to really draw a conclusion, it is necessary to compare collection methods similar to the ones you will use.
Nice video!
There is almost never a need to use array_key_exists unless you are testing for existence of a key AND want to get a return value of true if the value itself is null.
If you know values won't be null OR you wish to ignore them, isset is MUCH faster, especially at this scale of number of records
thank you...
ОтветитьGreat content 👌
Ответить