Java 8中的monadic编程是否较慢?以下是我的测试(使用权利偏差的任何一种,为每个计算创建新的实例).命令版本快1000倍.在Java8中如何编程monadicaly同时获得可比性能?

Main.java

public class Main {

    public static void main(String args[]){
        Main m = new Main();
        m.work();
        m.work2();
    }


    public void work(){
        final long start = System.nanoTime();
        final Either<Throwable,Integer> result =
                Try(this::getInput).flatMap((s) ->
                Try(this::getInput).flatMap((s2) ->
                parseInt(s).flatMap((i) ->
                parseInt(s2).map((i2) ->
                i + i2
                ))));
        final long end = System.nanoTime();
        result.map(this::println).leftMap(this::println);
        System.out.println((end-start)/1000+"us to execute");
    }

    public void work2(){
        Object result;
        final long start = System.nanoTime();
        try {
            final String s = getinput();
            final String s2 = getinput();

            final int i = parzeInt(s);
            final int i2 = parzeInt(s2);
            result = i + i2;
        }catch(Throwable t){
            result=t;
        }
        final long end = System.nanoTime();
        println(result);
        System.out.println((end-start)/1000+"us to execute");
    }

    public <A> A println(final A a){
        System.out.println(a);
        return a;
    }

    public  String getinput(){
        final Integer value = new Random().nextInt();
        if(value % 2 == 0) return "Surprise!!!";
        return value+"";
    }

    public Either<Throwable,Integer> parseInt(final String s){
        try{
            return Either.right(Integer.parseInt(s));
        }catch(final Throwable t){
            return Either.left(t);
        }
    }

    public Integer parzeInt(final String s){
        return Integer.parseInt(s);
    }
}

Either.java

public abstract class Either<L,R>
{
    public static <L,R> Either<L,R> left(final L l){
        return new Left(l);
    }

    public static <L,R> right(final R r){
        return new Right(r);
    }

    public static<L,R> toEither(final Optional<R> oR,final L l){
        return oR.isPresent() ? right(oR.get()) : left(l);
    }

    public static <R> Either<Throwable,R> Try(final supplier<R> sr){
        try{
            return right(sr.get());
        }catch(Throwable t){
            return left(t);
        }
    }

    public abstract <R2> Either<L,R2> flatMap(final Function<R,Either<L,R2>> f);

    public abstract  <R2> Either<L,R2> map(final Function<R,R2> f);

    public abstract  <L2> Either<L2,R> leftMap(final Function<L,L2> f);

    public abstract  Either<R,L> swap();

    public static class Left<L,R> extends Either<L,R> {
        final L l;

        private Left(final L l){
            this.l=l;
        }

        public <R2> Either<L,R2>> f){
            return (Either<L,R2>)this;
        }

        public <R2> Either<L,R2> f){
            return (Either<L,R2>)this;
        }

        public <L2> Either<L2,L2> f){
            return new Left(f.apply(l));
        }

        public Either<R,L> swap(){
            return new Right(l);
        }
    }

    public static class Right<L,R> {
        final R r;

        private Right(final R r){
            this.r=r;
        }

        public <R2> Either<L,R2>> f){
            return f.apply(r);
        }

        public <R2> Either<L,R2> f){
            return new Right(f.apply(r));
        }

        public <L2> Either<L2,L2> f){
            return (Either<L2,R>)this;
        }

        public Either<R,L> swap(){
            return new Left(r);
        }
    }
}

解决方法

虽然我不太明白你的努力 – 显然你正在使用地图的副作用,你没有任何替代方案可以从任何一种类型获取结果取消装箱 – 我已经测量了你的工作在JMH.你的使用Random是错误的,我纠正了.这是我使用的代码:
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@OperationsPerInvocation(Measure.SIZE)
@Warmup(iterations = 5,time = 1,timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5,timeUnit = TimeUnit.SECONDS)
@State(Scope.Thread)
@Fork(1)
public class Measure
{
  static final int SIZE = 1;

  @Benchmark public Either<Throwable,Integer> workMonadically() {
    final Either<Throwable,Integer> result =
        Try(this::getInput).flatMap((s) ->
            Try(this::getInput).flatMap((s2) ->
                parseInt(s).flatMap((i) ->
                    parseInt(s2).map((i2) ->
                            i + i2
                    ))));
    return result;
  }

  @Benchmark public Object workImperatively() {
    Object result;
    try {
      final String s = getinput();
      final String s2 = getinput();

      final int i = parzeInt(s);
      final int i2 = parzeInt(s2);
      result = i + i2;
    }catch(Throwable t){
      result=t;
    }
    return result;
  }

  public String getinput() {
    final Integer value = ThreadLocalRandom.current().nextInt();
    if (value % 2 == 0) return "Surprise!!!";
    return String.valueOf(value);
  }

  public Either<Throwable,Integer> parseInt(final String s){
    try{
      return Either.right(Integer.parseInt(s));
    }catch(final Throwable t){
      return Either.left(t);
    }
  }

  public Integer parzeInt(final String s){
    return Integer.parseInt(s);
  }

  public static abstract class Either<L,R>
  {
    public static <L,R> left(final L l){
      return new Left<>(l);
    }

    public static <L,R> right(final R r){
      return new Right<>(r);
    }

    public static<L,final L l){
      return oR.isPresent() ? right(oR.get()) : left(l);
    }

    public static <R> Either<Throwable,R> Try(final supplier<R> sr){
      try{
        return right(sr.get());
      }catch(Throwable t){
        return left(t);
      }
    }

    public abstract <R2> Either<L,R> {
      final L l;

      private Left(final L l){
        this.l=l;
      }

      @Override public <R2> Either<L,R2>> f){
        return (Either<L,R2>)this;
      }

      @Override public <R2> Either<L,R2> f){
        return (Either<L,R2>)this;
      }

      @Override public <L2> Either<L2,L2> f){
        return new Left<>(f.apply(l));
      }

      @Override public Either<R,L> swap(){
        return new Right<>(l);
      }
    }

    public static class Right<L,R> {
      final R r;

      private Right(final R r){
        this.r=r;
      }

      @Override public <R2> Either<L,R2>> f){
        return f.apply(r);
      }

      @Override public <R2> Either<L,R2> f){
        return new Right<>(f.apply(r));
      }

      @Override public <L2> Either<L2,L2> f){
        return (Either<L2,R>)this;
      }

      @Override public Either<R,L> swap(){
        return new Left<>(r);
      }
    }
  }
}

这是结果:

Benchmark                 Mode  Cnt     score     Error  Units
Measure.workImperatively  avgt    5  1646,874 ± 137,326  ns/op
Measure.workMonadically   avgt    5  1990,668 ± 281,646  ns/op

所以几乎没有区别.

在Java8中如何编程monadicaly同时获得可比性能?的更多相关文章

  1. 基于EJB技术的商务预订系统的开发

    用EJB结构开发的应用程序是可伸缩的、事务型的、多用户安全的。总的来说,EJB是一个组件事务监控的标准服务器端的组件模型。基于EJB技术的系统结构模型EJB结构是一个服务端组件结构,是一个层次性结构,其结构模型如图1所示。图2:商务预订系统的构架EntityBean是为了现实世界的对象建造的模型,这些对象通常是数据库的一些持久记录。

  2. js中‘!.’是什么意思

  3. InnoDB 和 MyISAM 引擎恢复数据库,使用 .frm、.ibd文件恢复数据库

  4. Error: Cannot find module ‘node:util‘问题解决

    控制台 安装 Vue-Cli 最后一步出现 Error: Cannot find module 'node:util' 问题解决方案1.问题C:\Windows\System32>cnpm install -g @vue/cli@4.0.3internal/modules/cjs/loader.js:638 throw err; &nbs

  5. yarn的安装和使用(全网最详细)

    一、yarn的简介:Yarn是facebook发布的一款取代npm的包管理工具。二、yarn的特点:速度超快。Yarn 缓存了每个下载过的包,所以再次使用时无需重复下载。 同时利用并行下载以最大化资源利用率,因此安装速度更快。超级安全。在执行代码之前,Yarn 会通过算法校验每个安装包的完整性。超级可靠。使用详细、简洁的锁文件格式和明确的安装算法,Yarn 能够保证在不同系统上无差异的工作。三、y

  6. 前端环境 本机可切换node多版本 问题源头是node使用的高版本

    前言投降投降 重头再来 重装环境 也就分分钟的事 偏要折腾 这下好了1天了 还没折腾出来问题的源头是node 使用的高版本 方案那就用 本机可切换多版本最终问题是因为nodejs的版本太高,导致的node-sass不兼容问题,我的node是v16.14.0的版本,项目中用了"node-sass": "^4.7.2"版本,无法匹配当前的node版本根据文章的提

  7. 宝塔Linux的FTP连接不上的解决方法

    宝塔Linux的FTP连接不上的解决方法常见的几个可能,建议先排查。1.注意内网IP和外网IP2.检查ftp服务是否启动 (面板首页即可看到)3.检查防火墙20端口 ftp 21端口及被动端口39000 - 40000是否放行 (如是腾讯云/阿里云等还需检查安全组)4.是否主动/被动模式都不能连接5.新建一个用户看是否能连接6.修改ftp配置文件 将ForcePassiveIP前面的#去掉 将19

  8. 扩展element-ui el-upload组件,实现复制粘贴上传图片文件,带图片预览功能

  9. 微信小程序canvas实现水平、垂直居中效果

    这篇文章主要介绍了小程序中canvas实现水平、垂直居中效果,本文图文实例代码相结合给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

  10. 使用HTML5做的导航条详细步骤

    这篇文章主要介绍了用HTML5做的导航条详细步骤,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

随机推荐

  1. 基于EJB技术的商务预订系统的开发

    用EJB结构开发的应用程序是可伸缩的、事务型的、多用户安全的。总的来说,EJB是一个组件事务监控的标准服务器端的组件模型。基于EJB技术的系统结构模型EJB结构是一个服务端组件结构,是一个层次性结构,其结构模型如图1所示。图2:商务预订系统的构架EntityBean是为了现实世界的对象建造的模型,这些对象通常是数据库的一些持久记录。

  2. Java利用POI实现导入导出Excel表格

    这篇文章主要为大家详细介绍了Java利用POI实现导入导出Excel表格,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  3. Mybatis分页插件PageHelper手写实现示例

    这篇文章主要为大家介绍了Mybatis分页插件PageHelper手写实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  4. (jsp/html)网页上嵌入播放器(常用播放器代码整理)

    网页上嵌入播放器,只要在HTML上添加以上代码就OK了,下面整理了一些常用的播放器代码,总有一款适合你,感兴趣的朋友可以参考下哈,希望对你有所帮助

  5. Java 阻塞队列BlockingQueue详解

    本文详细介绍了BlockingQueue家庭中的所有成员,包括他们各自的功能以及常见使用场景,通过实例代码介绍了Java 阻塞队列BlockingQueue的相关知识,需要的朋友可以参考下

  6. Java异常Exception详细讲解

    异常就是不正常,比如当我们身体出现了异常我们会根据身体情况选择喝开水、吃药、看病、等 异常处理方法。 java异常处理机制是我们java语言使用异常处理机制为程序提供了错误处理的能力,程序出现的错误,程序可以安全的退出,以保证程序正常的运行等

  7. Java Bean 作用域及它的几种类型介绍

    这篇文章主要介绍了Java Bean作用域及它的几种类型介绍,Spring框架作为一个管理Bean的IoC容器,那么Bean自然是Spring中的重要资源了,那Bean的作用域又是什么,接下来我们一起进入文章详细学习吧

  8. 面试突击之跨域问题的解决方案详解

    跨域问题本质是浏览器的一种保护机制,它的初衷是为了保证用户的安全,防止恶意网站窃取数据。那怎么解决这个问题呢?接下来我们一起来看

  9. Mybatis-Plus接口BaseMapper与Services使用详解

    这篇文章主要为大家介绍了Mybatis-Plus接口BaseMapper与Services使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  10. mybatis-plus雪花算法增强idworker的实现

    今天聊聊在mybatis-plus中引入分布式ID生成框架idworker,进一步增强实现生成分布式唯一ID,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部