Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Optimize this

Comments in 'Plugin Development' started by AndrewBit, Jan 14, 2016.

  1. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    I hate foreachs on tasks...
    PHP:
    $games = ["RB-1","RB-2","RB-3","RB-4","RB-5","RB-6","RB-7","RB-8","RB-9","RB-10"];
                        foreach(
    $games as $game){
                            if(
    $tile->getText()[0] == TextFormat::GRAY "[" TextFormat::WHITE $game TextFormat::GRAY "]"){
                                if(
    $onlinePlayers[$game] <= and $this->getPlugin()->getGameValue($game) == false){
                                    
    $tile->setText(TextFormat::GRAY "[" TextFormat::WHITE $game TextFormat::GRAY "]"TextFormat::BOLD TextFormat::GOLD "Join the game"TextFormat::BOLD TextFormat::GOLD "Online: " $onlinePlayers[$game] . "/" "6");
                                }
                                else{
                                    
    $tile->setText(TextFormat::GRAY "[" TextFormat::WHITE $game TextFormat::GRAY "]"TextFormat::BOLD TextFormat::GOLD "Game started"TextFormat::BOLD TextFormat::GOLD "Online: " $onlinePlayers[$game] . "/" "6");
                                }
                            }
                        }
    There is any method better than this?
    I was thinking in use in_array($tile->getText[0],$games), but, if I change $games arrays with TextFormat::WHITE . $game . TextFormat::GRAY . "]" I won't use $tile->getText[0] for get levels...

    That's so hard :eek:
  2. Guillaume351
    Offline

    Guillaume351 Active Member Plugin Developer

    Joined:
    Oct 11, 2013
    Posts:
    150
    Plugins:
    1
    Minecraft User:
    Guillaume351
    PHP:
           for($i 0$i 10$i++){
                
    $game "RB-".$i;
                if(
    $tile->getText()[0] == TextFormat::GRAY "[" TextFormat::WHITE $game TextFormat::GRAY "]"){
                    if(
    $onlinePlayers[$game] <= and !$this->getPlugin()->getGameValue($game)){
                        
    $tile->setText(TextFormat::GRAY "[" TextFormat::WHITE $game TextFormat::GRAY "]"TextFormat::BOLD TextFormat::GOLD "Join the game"TextFormat::BOLD TextFormat::GOLD "Online: " $onlinePlayers[$game] . "/" "6");
                    }
                    else{
                        
    $tile->setText(TextFormat::GRAY "[" TextFormat::WHITE $game TextFormat::GRAY "]"TextFormat::BOLD TextFormat::GOLD "Game started"TextFormat::BOLD TextFormat::GOLD "Online: " $onlinePlayers[$game] . "/" "6");
                    }
                }
            }
    Maybe that's 1% better.. What do you want to replace exactly ?
    AndrewBit likes this.
  3. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    TextFormat::clean($string);
    AndrewBit likes this.
  4. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    Thanks! :oops:
    * Wait, I also need to delete "[]" if I want get as well the level name...
    Is a good idea use str_replace with this?
  5. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Either that or
    PHP:
    substr($string1, -1);
    if it's always at the beginning and the end. (This would remove the first and the last character of the string)
    AndrewBit likes this.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    If you want optimization, maybe you should cache the valid signs in a chunk on ChunkLoadEvent and SignChangeEvent, and only process these signs in the task. And probably reduce task frequency.
  7. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    Well, the task is running every 10 ticks (20 is too much, I think).
    And I'm using SignChangeEvent (not ChunkLoadEvent) too, the task main objective is reload the online players and the game status.
  8. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    NO! foreach loop is much more faster than for loop!
    Every time you can, USE it.
    If you don't have an idea of how faster than for loop is it imagine that in my C# voxel game chunk loading with for loop took ~3 seconds while with foreach it takes ~0.45 seconds
    Guillaume351 likes this.
  9. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    I was thinking on that too...
    I think that for is a extension of while.

    But I didn't answer because I'm the newbie here x)
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Not really better if it makes code cleaner.
    Guillaume351 likes this.
  11. Guillaume351
    Offline

    Guillaume351 Active Member Plugin Developer

    Joined:
    Oct 11, 2013
    Posts:
    150
    Plugins:
    1
    Minecraft User:
    Guillaume351
    I thought it was slower to access multiple time to an array, thanks
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    With foreach, PHP loads the iteration with an internal array pointer. With for, PHP has to use an extra $i as the channel, and since here it also involves string concatenation, it might be slightly slower due to the passed nature of PHP.
    @EvolSoft I would just like to confirm that it is a fair test.
    Vaivez66, Guillaume351 and EvolSoft like this.

Share This Page

Advertisement