Lambdas or loops?

Status
This thread has been locked.

ElegantDashes

Feedback score
0
Posts
87
Reactions
32
Resources
0
So, I've recently been looking a lot at lambdas and how to use them in plugins.

Say i wanted to send a message to all staff that a player needed help?
I could either do it this way:

for(Player p : Bukkit.getOnlinePlayers()) {
if(p.hasPermisson("example.helpop")) {
p.sendMessage("A player needs help");
}
}

this is pretty standard, but I could also do it this way:

Bukkit.getOnlinePlayers().stream()
.filter(p -> p.hasPermission("example.helpop"))
.forEach(Player::sendMessage("A player needs help");

Now what would be better to use and what would be more efficent?
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

Killstreak702

Premium
Feedback score
1
Posts
227
Reactions
105
Resources
0
The for loop is much more easier to write, lambdas can be slightly more complicated to write obviously. For loops that include any boolean statement or anything (like if) I normally just make an ordinary for loop for it.
 

Hiry

Member
Supreme
Feedback score
6
Posts
131
Reactions
93
Resources
0
Both are inefficient, no need to loop through players and check for permissions.. just use
Code:
Bukkit.broadcast("A player needs your help", "example.helpop")
 

matthewp

Software Developer
Supreme
Feedback score
14
Posts
542
Reactions
503
Resources
0
Both are inefficient, no need to loop through players and check for permissions.. just use
Code:
Bukkit.broadcast("A player needs your help", "example.helpop")


That method never works for me half the time.
 

Garispl

Feedback score
2
Posts
44
Reactions
28
Resources
0
Both are inefficient, no need to loop through players and check for permissions.. just use
Code:
Bukkit.broadcast("A player needs your help", "example.helpop")

Code:
    @Override
    public int broadcast(String message, String permission) {
        int count = 0;
        Set<Permissible> permissibles = getPluginManager().getPermissionSubscriptions(permission);

        for (Permissible permissible : permissibles) {
            if (permissible instanceof CommandSender && permissible.hasPermission(permission)) {
                CommandSender user = (CommandSender) permissible;
                user.sendMessage(message);
                count++;
            }
        }

        return count;
    }
^^ The code from CraftServer (Implementation of the Server interface) I think You'll find either a for-loop or a lambda expression will be faster (By a few µs max)
 

Garispl

Feedback score
2
Posts
44
Reactions
28
Resources
0
Generally though, a for(;; ){} should be faster as it's less likely to modify the stack. However, #stream(..) will most likely result in a stack change for each item, so if performance is a requirement there might be an impact using stream. But.. stack changes are also relatively cheap - In the context of a plugin, anything should be fine, Your dealing w/ 10 or so calls a second, saving µs isn't needed.
 

LCastr0

Feedback score
0
Posts
15
Reactions
5
Resources
0
Although this thread doesn't get any answer since May 29th, I wanted to give a proper answer about this case.
I was reading stackoverflow and a user had a similar question, and one of the answers used JMH, which is a benchmark tool, and compared Stream vs For loops. At the end, the vanilla solution (for loops) was faster than Stream by 6.792 nanoseconds, which might not sound much, but that really depends on where you are using, so let's say you have trillions of things to be streamed, then those nanoseconds might make a difference.
I'm not gonna suggest you to keep using for loops, neither am I going to suggest you to use lambdas. It really depends on the situation. In your specific situation, for loops are faster.
Here is the stackoverflow thread if anyone is interested: https://stackoverflow.com/questions/22658322/java-8-performance-of-streams-vs-collections
 
Status
This thread has been locked.
Top