当前位置: 首页 > news >正文

DataInputStream数据读取 Vs ByteBuffer数据读取的巨大性能差距

背景:

今天在查找一个序列化和反序列化相关的问题时,意外发现使用DataInputStream读取和ByteBuffer读取之间性能相差巨大,本文就来记录下这两者在读取整数类型时的性能差异,以便在平时使用的过程中引起注意

DataInputStream数据读取 Vs ByteBuffer数据读取

我们会分别使用DataInputStream和ByteBuffer读取不同长度的字节数组,分别测试读取Short,Int,Long三类整数,然后分别看一下他们的性能

性能测试代码如下:

package org.example.jmh.deserialize;import static java.util.concurrent.TimeUnit.MICROSECONDS;
import static org.example.jmh.deserialize.DataUtil.createByteArray;import java.io.ByteArrayInputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;@Warmup(iterations = 2, time = 2)
@Measurement(iterations = 3, time = 10)
@Fork(1)
@OutputTimeUnit(MICROSECONDS)
public class ByteBufferVsByteArrayDataInputBenchmark {@State(Scope.Benchmark)public static class ByteBufferBackedDataInputState {ByteBuffer buffer;DataInput dataInput;@Param({"8", "32", "64", "128"})int size;byte[] data;public void createData() {this.data = createByteArray(size);}@Setup(Level.Trial)public void init() {createData();this.buffer = ByteBuffer.wrap(data);this.dataInput = new BufferDataInput(buffer);}void reset() {buffer.position(0);}}@State(Scope.Benchmark)public static class StreamBackedDataInpuState {ByteArrayInputStream bis;DataInput dataInput;@Param({"8", "32", "64", "128"})int size;byte[] data;public void createData() {this.data = createByteArray(size);}@Setup(Level.Trial)public void init() {createData();this.bis = new ByteArrayInputStream(data);this.dataInput = new DataInputStream(bis);}void reset() {bis.reset();}}@Benchmarkpublic void streamBackedDataInputReadShort(StreamBackedDataInpuState state, Blackhole bh) throws IOException {DataInput input = state.dataInput;int size = state.size;int pos = 0;while (pos < size) {bh.consume(input.readShort());pos += 2;}state.reset();}@Benchmarkpublic void bufferBackedDataInputReadShort(ByteBufferBackedDataInputState state, Blackhole bh) throws IOException {DataInput input = state.dataInput;int size = state.size;int pos = 0;while (pos < size) {bh.consume(input.readShort());pos += 2;}state.reset();}@Benchmarkpublic void streamBackedDataInputReadInt(StreamBackedDataInpuState state, Blackhole bh) throws IOException {DataInput input = state.dataInput;int size = state.size;int pos = 0;while (pos < size) {bh.consume(input.readInt());pos += 4;}state.reset();}@Benchmarkpublic void bufferBackedDataInputReadInt(ByteBufferBackedDataInputState state, Blackhole bh) throws IOException {DataInput input = state.dataInput;int size = state.size;int pos = 0;while (pos < size) {bh.consume(input.readInt());pos += 4;}state.reset();}@Benchmarkpublic void streamBackedDataInputReadLong(StreamBackedDataInpuState state, Blackhole bh) throws IOException {DataInput input = state.dataInput;int size = state.size;int pos = 0;while (pos < size) {bh.consume(input.readLong());pos += 8;}state.reset();}@Benchmarkpublic void bufferBackedDataInputReadLong(ByteBufferBackedDataInputState state, Blackhole bh) throws IOException {DataInput input = state.dataInput;int size = state.size;int pos = 0;while (pos < size) {bh.consume(input.readLong());pos += 8;}state.reset();}public static class BufferDataInput implements DataInput {private final ByteBuffer data;public BufferDataInput(ByteBuffer data) {this.data = data;}@Overridepublic void readFully(byte[] bytes) {}@Overridepublic void readFully(byte[] bytes, int i, int i1) {}@Overridepublic int skipBytes(int i) {data.position(data.position() + i);return data.position();}@Overridepublic boolean readBoolean() {return data.get() != 0;}@Overridepublic byte readByte() {return data.get();}@Overridepublic int readUnsignedByte() {return data.get() & 0xFF;}@Overridepublic short readShort() {return data.getShort();}@Overridepublic int readUnsignedShort() {return data.getShort() & 0xffff;}@Overridepublic char readChar() {return data.getChar();}@Overridepublic int readInt() {return data.getInt();}@Overridepublic long readLong() {return data.getLong();}@Overridepublic float readFloat() {return data.getFloat();}@Overridepublic double readDouble() {return data.getDouble();}@Overridepublic String readLine() {return null;}@Overridepublic String readUTF() {return null;}}}

jmh的测试结果:
在这里插入图片描述
从结果可以看出,不论是读取Short,Int还是Long,ByteBuffer都比DataInputStream有很大的性能优势,大概3-4倍的差异,那么你知道造成这种性能差距的原因是什么吗,留个悬念

相关文章:

DataInputStream数据读取 Vs ByteBuffer数据读取的巨大性能差距

背景&#xff1a; 今天在查找一个序列化和反序列化相关的问题时&#xff0c;意外发现使用DataInputStream读取和ByteBuffer读取之间性能相差巨大&#xff0c;本文就来记录下这两者在读取整数类型时的性能差异&#xff0c;以便在平时使用的过程中引起注意 DataInputStream数据…...

org.apache.flink.table.api.TableException: Sink does not exists

FlinkSQL_1.12_用DDL实现Kafka到MySQL的数据传输_实现按照条件进行过滤写入MySQL_flink从kafka拉取数据并过滤数据写入mysql_旧城里的阳光的博客-CSDN博客 参考这篇文章&#xff0c;写了kafka到mysql的代码例子&#xff0c;因为自己改了表结构&#xff0c;运行下面代码&#x…...

【多线程】CAS 详解

CAS 详解 一. 什么是 CAS二. CAS 的应用1. 实现原子类2. 实现自旋锁 三. CAS 的 ABA 问题四. 相关面试题 一. 什么是 CAS CAS: 全称Compare and swap&#xff0c;字面意思:”比较并交换“一个 CAS 涉及到以下操作&#xff1a; 我们假设内存中的原数据 V&#xff0c;旧的预期值…...

卷积神经网络实现咖啡豆分类 - P7

&#x1f368; 本文为&#x1f517;365天深度学习训练营 中的学习记录博客&#x1f356; 原作者&#xff1a;K同学啊 | 接辅导、项目定制&#x1f680; 文章来源&#xff1a;K同学的学习圈子 目录 环境步骤环境设置包引用全局设备对象 数据准备查看图像的信息制作数据集 模型设…...

C++之默认与自定义构造函数问题(二百一十七)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 人生格言&#xff1a; 人生…...

Docker从认识到实践再到底层原理(五)|Docker镜像

前言 那么这里博主先安利一些干货满满的专栏了&#xff01; 首先是博主的高质量博客的汇总&#xff0c;这个专栏里面的博客&#xff0c;都是博主最最用心写的一部分&#xff0c;干货满满&#xff0c;希望对大家有帮助。 高质量博客汇总 然后就是博主最近最花时间的一个专栏…...

【Flowable】任务监听器(五)

前言 之前有需要使用到Flowable&#xff0c;鉴于网上的资料不是很多也不是很全也是捣鼓了半天&#xff0c;因此争取能在这里简单分享一下经验&#xff0c;帮助有需要的朋友&#xff0c;也非常欢迎大家指出不足的地方。 一、监听器 在Flowable中&#xff0c;我们可以使用监听…...

spring-kafka中ContainerProperties.AckMode详解

近期&#xff0c;我们线上遇到了一个性能问题&#xff0c;几乎快引起线上故障&#xff0c;后来仅仅是修改了一行代码&#xff0c;性能就提升了几十倍。一行代码几十倍&#xff0c;数据听起来很夸张&#xff0c;不过这是真实的数据&#xff0c;线上错误的配置的确有可能导致性能…...

【rpc】Dubbo和Zookeeper结合使用,它们的作用与联系(通俗易懂,一文理解)

目录 Dubbo是什么&#xff1f; 把系统模块变成分布式&#xff0c;有哪些好处&#xff0c;本来能在一台机子上运行&#xff0c;为什么还要远程调用 Zookeeper是什么&#xff1f; 它们进行配合使用时&#xff0c;之间的关系 服务注册 服务发现 动态地址管理 Dubbo是…...

ChatGPT的未来

随着人工智能的快速发展&#xff0c;ChatGPT作为一种自然语言生成模型&#xff0c;在各个领域都展现出了巨大的潜力。它不仅可以用于日常对话、创意助手和知识查询&#xff0c;还可以应用于教育、医疗、商业等各个领域&#xff0c;为人们带来更多便利和创新。 在教育领域&#…...

Pytorch模型转ONNX部署

开始以为会很困难&#xff0c;但是其实非常方便&#xff0c;下边分两步走&#xff1a;1. pytorch模型转onnx&#xff1b;2. 使用onnx进行inference 0. 准备工作 0.1 安装onnx 安装onnx和onnxruntime&#xff0c;onnx貌似是个环境。。倒是没有直接使用&#xff0c;onnxruntim…...

k8s优雅停服

在应用程序的整个生命周期中&#xff0c;正在运行的 pod 会由于多种原因而终止。在某些情况下&#xff0c;Kubernetes 会因用户输入&#xff08;例如更新或删除 Deployment 时&#xff09;而终止 pod。在其他情况下&#xff0c;Kubernetes 需要释放给定节点上的资源时会终止 po…...

面试题五:computed的使用

题记 大部分的工作中使用computed的频次很低的&#xff0c;所以今天拿出来一文对于computed进行详细的介绍&#xff0c;因为Vue的灵魂之一就是computed。 模板内的表达式非常便利&#xff0c;但是设计它们的初衷是用于简单运算的。在模板中放入太多的逻辑会让模板过重且难以维护…...

完美的分布式监控系统 Prometheus与优雅的开源可视化平台 Grafana

1、之间的关系 prometheus与grafana之间是相辅相成的关系。简而言之Grafana作为可视化的平台&#xff0c;平台的数据从Prometheus中取到来进行仪表盘的展示。而Prometheus这源源不断的给Grafana提供数据的支持。 Prometheus是一个开源的系统监控和报警系统&#xff0c;能够监…...

黑马JVM总结(九)

&#xff08;1&#xff09;StringTable_调优1 我们知道StringTable底层是一个哈希表&#xff0c;哈希表的性能是跟它的大小相关的&#xff0c;如果哈希表这个桶的个数比较多&#xff0c;元素相对分散&#xff0c;哈希碰撞的几率就会减少&#xff0c;查找的速度较快&#xff0c…...

如何使用 RunwayML 进行创意 AI 创作

标题&#xff1a;如何使用 RunwayML 进行创意 AI 创作 介绍 RunwayML 是一个基于浏览器的人工智能创作工具&#xff0c;可让用户使用各种 AI 功能来生成图像、视频、音乐、文字和其他创意内容。RunwayML 的功能包括&#xff1a; * 图像生成&#xff1a;使用生成式对抗网络 (…...

【css】能被4整除 css :class,判断一个数能否被另外一个数整除,余数

判断一个数能否被另外一个数整除 一个数能被4整除的表达式可以表示为&#xff1a;num%40&#xff0c;其中&#xff0c;num为待判断的数&#xff0c;% 为取模运算符&#xff0c;为等于运算符。这个表达式的意思是&#xff0c;如果num除以4的余数为0&#xff0c;则返回true&…...

ChatGPT与日本首相交流核废水事件-精准Prompt...

了解更多请点击&#xff1a;ChatGPT与日本首相交流核废水事件-精准Prompt...https://mp.weixin.qq.com/s?__bizMzg2NDY3NjY5NA&mid2247490070&idx1&snebdc608acd419bb3e71ca46acee04890&chksmce64e42ff9136d39743d16059e2c9509cc799a7b15e8f4d4f71caa25968554…...

关于 firefox 不能访问 http 的解决

情景&#xff1a; 我在虚拟机 192.168.x.111 上配置了 DNS 服务器&#xff0c;在 kali 上设置 192.168.x.111 为 DNS 服务器后&#xff0c;使用 firefox 地址栏搜索域名 www.xxx.com &#xff0c;访问在 192.168.x.111 搭建的网站&#xff0c;本来经 192.168.x.111 DNS 服务器解…...

68、Spring Data JPA 的 方法名关键字查询

★ 方法名关键字查询&#xff08;全自动&#xff09; &#xff08;1&#xff09;继承 CrudRepository 接口 的 DAO 组件可按特定规则来定义查询方法&#xff0c;只要这些查询方法的 方法名 遵守特定的规则&#xff0c;Spring Data 将会自动为这些方法生成 查询语句、提供 方法…...

Python爬虫实战:研究MechanicalSoup库相关技术

一、MechanicalSoup 库概述 1.1 库简介 MechanicalSoup 是一个 Python 库,专为自动化交互网站而设计。它结合了 requests 的 HTTP 请求能力和 BeautifulSoup 的 HTML 解析能力,提供了直观的 API,让我们可以像人类用户一样浏览网页、填写表单和提交请求。 1.2 主要功能特点…...

Linux应用开发之网络套接字编程(实例篇)

服务端与客户端单连接 服务端代码 #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <pthread.h> …...

Chapter03-Authentication vulnerabilities

文章目录 1. 身份验证简介1.1 What is authentication1.2 difference between authentication and authorization1.3 身份验证机制失效的原因1.4 身份验证机制失效的影响 2. 基于登录功能的漏洞2.1 密码爆破2.2 用户名枚举2.3 有缺陷的暴力破解防护2.3.1 如果用户登录尝试失败次…...

K8S认证|CKS题库+答案| 11. AppArmor

目录 11. AppArmor 免费获取并激活 CKA_v1.31_模拟系统 题目 开始操作&#xff1a; 1&#xff09;、切换集群 2&#xff09;、切换节点 3&#xff09;、切换到 apparmor 的目录 4&#xff09;、执行 apparmor 策略模块 5&#xff09;、修改 pod 文件 6&#xff09;、…...

Cesium1.95中高性能加载1500个点

一、基本方式&#xff1a; 图标使用.png比.svg性能要好 <template><div id"cesiumContainer"></div><div class"toolbar"><button id"resetButton">重新生成点</button><span id"countDisplay&qu…...

el-switch文字内置

el-switch文字内置 效果 vue <div style"color:#ffffff;font-size:14px;float:left;margin-bottom:5px;margin-right:5px;">自动加载</div> <el-switch v-model"value" active-color"#3E99FB" inactive-color"#DCDFE6"…...

生成 Git SSH 证书

&#x1f511; 1. ​​生成 SSH 密钥对​​ 在终端&#xff08;Windows 使用 Git Bash&#xff0c;Mac/Linux 使用 Terminal&#xff09;执行命令&#xff1a; ssh-keygen -t rsa -b 4096 -C "your_emailexample.com" ​​参数说明​​&#xff1a; -t rsa&#x…...

Ascend NPU上适配Step-Audio模型

1 概述 1.1 简述 Step-Audio 是业界首个集语音理解与生成控制一体化的产品级开源实时语音对话系统&#xff0c;支持多语言对话&#xff08;如 中文&#xff0c;英文&#xff0c;日语&#xff09;&#xff0c;语音情感&#xff08;如 开心&#xff0c;悲伤&#xff09;&#x…...

搭建DNS域名解析服务器(正向解析资源文件)

正向解析资源文件 1&#xff09;准备工作 服务端及客户端都关闭安全软件 [rootlocalhost ~]# systemctl stop firewalld [rootlocalhost ~]# setenforce 0 2&#xff09;服务端安装软件&#xff1a;bind 1.配置yum源 [rootlocalhost ~]# cat /etc/yum.repos.d/base.repo [Base…...

Python Einops库:深度学习中的张量操作革命

Einops&#xff08;爱因斯坦操作库&#xff09;就像给张量操作戴上了一副"语义眼镜"——让你用人类能理解的方式告诉计算机如何操作多维数组。这个基于爱因斯坦求和约定的库&#xff0c;用类似自然语言的表达式替代了晦涩的API调用&#xff0c;彻底改变了深度学习工程…...