切換
舊版
前往
大廳
主題

平行處理的寫法反而拖累效率

此間半開一盞茶 | 2019-10-17 00:28:38 | 巴幣 0 | 人氣 140

舊寫法,跑完8秒
IntStream.range(0, 16).forEach(x -> {
    IntStream.range(0, 128).forEach(y -> {
        IntStream.range(0, 16).forEach(z -> {
            BlockState b = chunk.getBlock(x, y, z).getState();
            if(Fo.isOreMa(b.getType())) {
                yml.set(chunk.getWorld().getName() + "." +
                                b.getX() + "." + b.getY() + "." + b.getZ(),
                        Fo.getByType(b.getType()).name());
            }
        });
    });
});
NMS新寫法跑完5秒
ChunkSection[] cs = ((CraftChunk) chunk).getHandle().getSections();
Arrays.asList(cs).stream().filter(chunkSection ->
        chunkSection != null).filter(chunkSection ->
        chunkSection.getBlocks().a(CraftMagicNumbers.getBlock(Material.GOLD_ORE).getBlockData()) ||
                chunkSection.getBlocks().a(CraftMagicNumbers.getBlock(Material.IRON_ORE).getBlockData()) ||
                chunkSection.getBlocks().a(CraftMagicNumbers.getBlock(Material.COAL_ORE).getBlockData()) ||
                chunkSection.getBlocks().a(CraftMagicNumbers.getBlock(Material.DIAMOND_ORE).getBlockData()) ||
                chunkSection.getBlocks().a(CraftMagicNumbers.getBlock(Material.LAPIS_ORE).getBlockData()) ||
                chunkSection.getBlocks().a(CraftMagicNumbers.getBlock(Material.REDSTONE_ORE).getBlockData()) ||
                chunkSection.getBlocks().a(CraftMagicNumbers.getBlock(Material.EMERALD_ORE).getBlockData()) ||
                chunkSection.getBlocks().a(CraftMagicNumbers.getBlock(Material.NETHER_QUARTZ_ORE).getBlockData())).
        collect(Collectors.toList()).forEach(chunkSection -> {
    IntStream.range(0, 16).forEach(x -> {
        IntStream.range(0, 16).forEach(y -> {
            IntStream.range(0, 16).forEach(z -> {
                Material type = CraftMagicNumbers.getMaterial(chunkSection.getType(x,y,z).getBlock());
                if(Fo.isOreMa(type)) {
                    int x0 = chunk.getX()*16;
                    int z0 = chunk.getZ()*16;
                    int y0 = chunkSection.getYPosition();
                    yml.set(chunk.getWorld().getName() + "." +
                                    (x0+x) + "." + (y0+y) + "." + (z0+z),
                            Fo.getByType(type).name());
                }
            });
        });
    });
});
NMS新寫法+平行處理跑完16秒
ChunkSection[] cs = ((CraftChunk) chunk).getHandle().getSections();
Arrays.asList(cs).parallelStream().filter(chunkSection ->
        chunkSection != null).filter(chunkSection ->
        chunkSection.getBlocks().a(CraftMagicNumbers.getBlock(Material.GOLD_ORE).getBlockData()) ||
                chunkSection.getBlocks().a(CraftMagicNumbers.getBlock(Material.IRON_ORE).getBlockData()) ||
                chunkSection.getBlocks().a(CraftMagicNumbers.getBlock(Material.COAL_ORE).getBlockData()) ||
                chunkSection.getBlocks().a(CraftMagicNumbers.getBlock(Material.DIAMOND_ORE).getBlockData()) ||
                chunkSection.getBlocks().a(CraftMagicNumbers.getBlock(Material.LAPIS_ORE).getBlockData()) ||
                chunkSection.getBlocks().a(CraftMagicNumbers.getBlock(Material.REDSTONE_ORE).getBlockData()) ||
                chunkSection.getBlocks().a(CraftMagicNumbers.getBlock(Material.EMERALD_ORE).getBlockData()) ||
                chunkSection.getBlocks().a(CraftMagicNumbers.getBlock(Material.NETHER_QUARTZ_ORE).getBlockData())).
        collect(Collectors.toList()).parallelStream().forEach(chunkSection -> {
    IntStream.range(0, 16).forEach(x -> {
        IntStream.range(0, 16).forEach(y -> {
            IntStream.range(0, 16).forEach(z -> {
                Material type = CraftMagicNumbers.getMaterial(chunkSection.getType(x,y,z).getBlock());
                if(Fo.isOreMa(type)) {
                    int x0 = chunk.getX()*16;
                    int z0 = chunk.getZ()*16;
                    int y0 = chunkSection.getYPosition();
                    yml.set(chunk.getWorld().getName() + "." +
                                    (x0+x) + "." + (y0+y) + "." + (z0+z),
                            Fo.getByType(type).name());
                }
            });
        });
    });
});
看來想要用平行處理似乎還要考慮其他條件
測試處理器i5-2410M
送禮物贊助創作者 !
0
留言

創作回應