StringBuilder 和 String拼接10万个字符串的速度测试差别太大了


/*** StringBuilder 和 String拼接10万个字符串的速度测试差别太大了* String耗时毫秒: 32693* StringBuilder耗时毫秒: 16*/
public class Demo7 {public static void main(String[] args) {long lsg = getString();System.out.println("String耗时毫秒: " + lsg);long sb = getSb();System.out.println("StringBuilder耗时毫秒: " + sb);System.out.println("String 比 StringBuilder多用耗时:" + (lsg - sb));}/*** StringBuilder 拼接十万个字符串的耗时* @return*/private static long getSb() {//起始时间long start = System.currentTimeMillis();StringBuilder sb = new StringBuilder();for (int i = 0; i <= 100000; i++) {sb.append(i);}//结束时间long end = System.currentTimeMillis();//返回共用耗时毫秒return end - start;}/*** string 拼接十万个字符串的耗时* @return 返回共用耗时毫秒*/private static long getString() {//起始时间long start = System.currentTimeMillis();String str = "";for (int i = 0; i <= 100000; i++) {str += i;}//结束时间long end = System.currentTimeMillis();//返回共用耗时毫秒return end - start;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/442838.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

html 浮动窗口置顶,jQuery简单实现页面元素置顶时悬浮效果示例

本文实例讲述了jQuery简单实现页面元素置顶时悬浮效果的方法。分享给大家供大家参考&#xff0c;具体如下&#xff1a;一、JS Code&#xff1a;$.fn.smartFloat function () {var position function (element) {var top element.position().top, pos element.css("pos…

键盘录入,替换敏感词

import java.util.Scanner;/*** 键盘录入&#xff0c;替换敏感词*/ public class Demo6 {public static void main(String[] args) {Scanner scanner new Scanner(System.in);System.out.println("请输入一个字符串&#xff0c;列如&#xff1a;去你TMD阿巴阿巴TMD阿巴阿…

html悬停放大图片代码,html – 放大图像并在悬停时显示div

在图像悬停时,我想放大图像并显示带有透明背景的div.这是我的代码.在下面的示例中,当我将鼠标悬停在.Image类中的图像上时,我想要缩放它,并希望在div的中心显示类.mylink的链接.我能够放大悬停但是当我为.text添加样式时,它不再放大图像.HTML&#xff1a;linkCSS&#xff1a;#B…

使用字符串切割,使手机号中间四位隐藏

/*** 使用字符串切割&#xff0c;使手机号中间四位隐藏*/ public class Demo5 {public static void main(String[] args) {//随便模拟一个手机号String str "18857552632";//切割前三位String s1 str.substring(0, 3);//切割后四位String s2 str.substring(7);Sys…

夜间模式html,夜间模式.html

demo.dark {background-color: #363636;filter: invert(100) hue-rotate(180deg);}.dark img {filter: invert(100) hue-rotate(180deg);}Switch theme一个第三方的 GMail 的 Web 客户端。百度链接var darkSwitch falsedocument.getElementById(switch).addEventListener(clic…

判断一个字符串大写小写,和数字出现的次数

/*** 判断一个字符串大写小写&#xff0c;和数字出现的次数*/ public class Demo4 {public static void main(String[] args) {String str "A1baC51-*/";//将字符串转为字符数组&#xff1b;char[] chars str.toCharArray();int bigCount 0, smallCount 0, numCo…

html bootstrap复选框全选,javascript+bootstrap+html实现层级多选框全层全选和多选功能代码实例...

想做一个先按层级排序并可以多选的功能&#xff0c;首先倾向于用多层标签式的&#xff0c;直接选定加在文本域里&#xff0c;接下来通过本文给大家介绍htmljavascriptbootstrap实现层级多选框全层全选和多选功能&#xff0c;需要的朋友参考下想做一个先按层级排序并可以多选的功…

最简单的控制台登录小案例,适合初学者

import java.util.Scanner;/*** 登录小案例*/ public class Demo2 {public static void main(String[] args) {String username "abc";String passworld "123";Scanner scanner new Scanner(System.in);for (int i 0; i < 3; i) {System.out.printl…

html5常用插件大全,前端常用插件utils汇总

发布于 2020-03-06工具库 || 数据处理表单验证---jquery图片懒加载---JavaScript---vue---react图片预览类似朋友圈满足聊天递增图片的需求---vue文件上传---JavaScript---vue单选框/复选框相关---jquery选择框tree树形---jquery无限滚动---vue列表拖拽---vue---react元素拖曳自…

一维数组求最大值,和三元运算符运算源码

public class Method3 {public static void main(String[] args) {//求三个数的最大数getMax(1, 3, 5);int[] arr {1, 564, 4, 54, 4565, 455, 6};//求数组最大值getArrayMax(arr);}/*求数组中的最大值*/private static void getArrayMax(int[] arr) {int max arr[0];for (in…

刚学计算机先学什么好,计算机语言入门先学什么?

刚想开始计算机学习的小白难免会有这样的困惑&#xff1a;计算机语言入门先学什么&#xff1f;简单来讲&#xff0c;初学者需要先了解各种计算机语言&#xff0c;熟悉计算机组成原理&#xff0c;学习数据结构与算法、数据库、基础语法等等&#xff0c;这些都是必须经历的基础学…

properties(map)增.删.改.查.遍历

import java.util.Map; import java.util.Properties; import java.util.Set;/*** properties&#xff08;map&#xff09;增.删.改.查.遍历*/ public class Demo1 {public static void main(String[] args) {Properties prop new Properties();System.out.println("添加…

判断一个字符串在另一个字符串中出现的次数

public class Demo2 {public static void main(String[] args) {String s1 "woaiheima,heimabutongyubaima,wulunheimahaishibaima,zhaodaogongzuojiushihaoma";String s2 "heima";/*** indexof从0开始找&#xff0c;找到就索引后移一位&#xff0c;统计…

xml控制html样式,XML与CSS综合设置实例

XML与CSS综合设置实例HTTP代码如下&#xff1a;charset "utf-8";charset "utf-8";bookname{display:block;font-size:36px;font-weight:bold;font-family:宋体;text-align:center;}bieming{display:block;font-size:20px;font-weight:normal;font-family:宋…

properties 特有的方法

/*** properties 特有的方法*/ public class Demo2 {public static void main(String[] args) {Properties prop new Properties();//添加prop.setProperty("a", "宝马");prop.setProperty("b", "奔驰");prop.setProperty("c&qu…

html 设置请求头,http请求头和响应头设置

windows中文操作系统默认gbk编码nodejs服务器默认返回utf-8content-type类型:text/palin; 普通文本类型text/html; html文档类型 比如res.end("段落"),后端返回的html文件,其实都是读取里面的内容作为字符串返回,或者二进制流数据.文件的读写就是流操作text/xml ; xm…

* 将字符串日期时间格式,转为毫秒

import java.text.ParseException; import java.text.SimpleDateFormat;/*** author silence* 将字符串日期时间格式&#xff0c;转为毫秒*/ public class Demo1 {public static void main(String[] args) throws ParseException {String start "2020年11月11日 0:0:0&qu…

幼儿园体育游戏电子计算机教案,幼儿园体育游戏教案3篇

幼儿园体育游戏教案3篇让幼儿通过各种体能游戏,锻炼走、跑、跳、爬等的能力,发展身体的灵活性和平衡能力。具有一定的竞争意识和集体荣誉感。以下是小编精心整理的幼儿园体育游戏教案的相关教案&#xff0c;老师们快来欣赏学习吧!幼儿园体育游戏教案范文一体能游戏活动目标:1、…

日期时间格式转化为方便理解的格式

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date;/*** author silence* 日期时间格式转化为方便理解的格式*/ public class Demo1 {public static void main(String[] args) throws ParseException {//获得当前时间Date date new…

html中js方法中如何传递本元素对象,JS HTML DOM (文档对象模型)

DOM简介当网页加载时&#xff0c;浏览器会创建页面的文档对象模型(DOM)。通过DOM&#xff0c;JS可以对HTML实现以下操作&#xff1a;改变页面中的所有HTML元素。改变页面中的所有HTML属性。改变页面中的所有CSS样式。对页面中的所有事件作出反应。DOM HTML获得HTML属性的三种方…