《java提高数据导入效率优化思路》
写在前边的实现需求:
1.总共10万个电话号码;
2.电话号码中有重复和错误;
3.查找出正确的号码(不重复);
一、优化前的实现方式:
1.先用正则过滤一遍10万条数据,找出错误的;
2.用List.Contains验证重复数据,List.Add添加不重复数据;
3.最终从List中取出正确的数据。
public class appMain {
final static int _capacity = 1000000;
final static Random rand = new Random(System.currentTimeMillis() + _capacity);
static ArrayList<String> list = new ArrayList<String>(_capacity);
static ArrayList<String> newlist = new ArrayList<String>(_capacity);
public static void main(String[] args) throws InterruptedException {
long ts = System.currentTimeMillis();
int modVal = _capacity / 3;
for (int i = 0; i < _capacity; i++) {
rand.setSeed(i);
list.add(Integer.toString(Math.abs(rand.nextInt() % modVal)));
}
ts = System.currentTimeMillis() - ts;
System.out.println("生成时间 :" + ts);
test1();
}
static void test1() {
newlist.clear();
int repetition = 0;
long ts = System.currentTimeMillis();
for (String s : list) {
if (!newlist.contains(s))
newlist.add(s);
else {
repetition++;
}
}
ts = System.currentTimeMillis() - ts;
System.out.println("------ 插入检查方法 -------");
System.out.println("查找时间 :" + ts);
System.out.println("重复 :" + repetition);
System.out.println("正确 :" + newlist.size());
}
}
优化前执行结果:
/*
条件:capacity = 100000
结果:
生成时间 :33
------ 插入检查方法 -------
查找时间 :6612
重复 :76871
正确 :23129
------ 排序检查方法 -------
查找时间 :91
重复 :76871
正确 :23129
*/
使用以上方式做导入的话数据量一旦超过5w以上马上出现假死状态,故肯定不可取,所以有了下边的优化。
二、优化后的实现方式:
1.先对10万数据排序;
2.对比前后两条数据(这个我之后会详细说明为什么这么做);
3.筛选出正确数据。
public class appMain {
final static int _capacity = 1000000;
final static Random rand = new Random(System.currentTimeMillis() + _capacity);
static ArrayList<String> list = new ArrayList<String>(_capacity);
static ArrayList<String> newlist = new ArrayList<String>(_capacity);
public static void main(String[] args) throws InterruptedException {
long ts = System.currentTimeMillis();
int modVal = _capacity / 3;
for (int i = 0; i < _capacity; i++) {
rand.setSeed(i);
list.add(Integer.toString(Math.abs(rand.nextInt() % modVal)));
}
ts = System.currentTimeMillis() - ts;
System.out.println("生成时间 :" + ts);
test2();
}
static void test2() {
newlist.clear();
int repetition = 0;
long ts = System.currentTimeMillis();
Collections.sort(list);
String str = list.get(0);
int max = list.size();
for (int i = 1; i < max; i++) {
if (str.equals(list.get(i))) {
repetition++;
continue;
}
newlist.add(str);
str = list.get(i);
}
newlist.add(str);
ts = System.currentTimeMillis() - ts;
System.out.println("------ 排序检查方法 -------");
System.out.println("查找时间 :" + ts);
System.out.println("重复 :" + repetition);
System.out.println("正确 :" + newlist.size());
}
}
优化后执行结果:
/*
条件:capacity = 1000000
结果:
生成时间 :392
------ 插入检查方法 -------
查找时间 :1033818
重复 :703036
正确 :296964
------ 排序检查方法 -------
查找时间 :1367
重复 :703036
正确 :296964
*/
当数据量达到10万条的时候,查找时间比差不多90倍的差距了;当数据量达到100万时,我这边测试数据已经卡死在test1(),而test2()依然能在数十秒内反馈结果。
下边来简单解剖下源码:
Collections.sort(list);
String str = list.get(0);
int max = list.size();
for (int i = 1; i < max; i++) {
if (str.equals(list.get(i))) {
repetition++;
continue;
}
newlist.add(str);
str = list.get(i);
}
Line 1:排序,加入list排序后的结果是[1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
Line 2:初始str = 1;
从Line 4开始进入循环:
Line 5:判断str是否和当先selector值相等(暂借我们认为list.get(i)是一个指针),如果相等则跳过以下步骤进入下一个循环
Line 9:将str = 1,加入newlist尾
Line10:将当前selector值赋给str,此时str=2,进入下一个循环
...
这种语言解释我个人觉得特别麻烦,我还是写段代码让程序告诉你它怎么执行的。
public class appList {
static ArrayList<String> list = new ArrayList<String>();
static ArrayList<String> newlist = new ArrayList<String>();
public static void main(String[] args) {
for (int i = 1; i < 5 + 1; i++) {
for (int j = 0; j < i; j++) {
list.add(Integer.toString(i));
}
}
System.out.println("list初始值 " + list.toString());
// print输出值 [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
String str = list.get(0);
int max = list.size();
for (int i = 1; i < max; i++) {
Print(i);
if (str.equals(list.get(i))) {
PrintNew();
continue;
}
newlist.add(str);
System.out.println("add\t" + str);
str = list.get(i);
PrintNew();
}
newlist.add(str);
System.out.println("add\t" + str);
PrintNew();
System.out.println("newlist值 " + newlist.toString());
// print输出值 [1, 2, 3, 4, 5]
}
static void PrintNew(){
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("newlist\t");
for (int i = 0; i < newlist.size(); i++) {
stringBuilder.append(newlist.get(i));
stringBuilder.append(",");
}
System.out.println(stringBuilder.toString());
System.out.println();
}
static void Print(int pos) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("list\t");
for (int i = 0; i < list.size(); i++) {
if (i == pos) {
stringBuilder.append("[");
stringBuilder.append(list.get(i));
stringBuilder.append("],");
} else {
stringBuilder.append(list.get(i));
stringBuilder.append(",");
}
}
System.out.println(stringBuilder.toString());
}
执行结果:
list初始值 [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
list 1,[2],2,3,3,3,4,4,4,4,5,5,5,5,5,
add 1
newlist 1, list 1,2,[2],3,3,3,4,4,4,4,5,5,5,5,5,
newlist 1, list 1,2,2,[3],3,3,4,4,4,4,5,5,5,5,5,
add 2
newlist 1,2, list 1,2,2,3,[3],3,4,4,4,4,5,5,5,5,5,
newlist 1,2, list 1,2,2,3,3,[3],4,4,4,4,5,5,5,5,5,
newlist 1,2, list 1,2,2,3,3,3,[4],4,4,4,5,5,5,5,5,
add 3
newlist 1,2,3, list 1,2,2,3,3,3,4,[4],4,4,5,5,5,5,5,
newlist 1,2,3, list 1,2,2,3,3,3,4,4,[4],4,5,5,5,5,5,
newlist 1,2,3, list 1,2,2,3,3,3,4,4,4,[4],5,5,5,5,5,
newlist 1,2,3, list 1,2,2,3,3,3,4,4,4,4,[5],5,5,5,5,
add 4
newlist 1,2,3,4, list 1,2,2,3,3,3,4,4,4,4,5,[5],5,5,5,
newlist 1,2,3,4, list 1,2,2,3,3,3,4,4,4,4,5,5,[5],5,5,
newlist 1,2,3,4, list 1,2,2,3,3,3,4,4,4,4,5,5,5,[5],5,
newlist 1,2,3,4, list 1,2,2,3,3,3,4,4,4,4,5,5,5,5,[5],
newlist 1,2,3,4, add 5
newlist 1,2,3,4,5, newlist值 [1, 2, 3, 4, 5]
《java提高数据导入效率优化思路》的更多相关文章
- 简单物联网:外网访问内网路由器下树莓派Flask服务器
最近做一个小东西,大概过程就是想在教室,宿舍控制实验室的一些设备. 已经在树莓上搭了一个轻量的flask服务器,在实验室的路由器下,任何设备都是可以访问的:但是有一些限制条件,比如我想在宿舍控制我种花 ...
- 利用ssh反向代理以及autossh实现从外网连接内网服务器
前言 最近遇到这样一个问题,我在实验室架设了一台服务器,给师弟或者小伙伴练习Linux用,然后平时在实验室这边直接连接是没有问题的,都是内网嘛.但是回到宿舍问题出来了,使用校园网的童鞋还是能连接上,使 ...
- 外网访问内网Docker容器
外网访问内网Docker容器 本地安装了Docker容器,只能在局域网内访问,怎样从外网也能访问本地Docker容器? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Docker容器 ...
- 外网访问内网SpringBoot
外网访问内网SpringBoot 本地安装了SpringBoot,只能在局域网内访问,怎样从外网也能访问本地SpringBoot? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装Java 1 ...
- 外网访问内网Elasticsearch WEB
外网访问内网Elasticsearch WEB 本地安装了Elasticsearch,只能在局域网内访问其WEB,怎样从外网也能访问本地Elasticsearch? 本文将介绍具体的实现步骤. 1. ...
- 怎样从外网访问内网Rails
外网访问内网Rails 本地安装了Rails,只能在局域网内访问,怎样从外网也能访问本地Rails? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Rails 默认安装的Rails端口 ...
- 怎样从外网访问内网Memcached数据库
外网访问内网Memcached数据库 本地安装了Memcached数据库,只能在局域网内访问,怎样从外网也能访问本地Memcached数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装 ...
- 怎样从外网访问内网CouchDB数据库
外网访问内网CouchDB数据库 本地安装了CouchDB数据库,只能在局域网内访问,怎样从外网也能访问本地CouchDB数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Cou ...
- 怎样从外网访问内网DB2数据库
外网访问内网DB2数据库 本地安装了DB2数据库,只能在局域网内访问,怎样从外网也能访问本地DB2数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动DB2数据库 默认安装的DB2 ...
- 怎样从外网访问内网OpenLDAP数据库
外网访问内网OpenLDAP数据库 本地安装了OpenLDAP数据库,只能在局域网内访问,怎样从外网也能访问本地OpenLDAP数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动 ...
随机推荐
- powdesigner建表
默认打开powerDesigner时,创建table对应的自动生成sql语句没有注释. 方法1.comment注释信息 在Columns标签下,一排按钮中找到倒数第2个按钮:Customize Col ...
- Codeforce-A-Two distinct points(暴力)
output standard output You are given two segments [l1;r1][l1;r1] and [l2;r2][l2;r2] on the xx-axis. ...
- Codeforces 277E
按边建模,二叉树一条入边两条出边 判断就要用到mcmf的好处了 #include<bits/stdc++.h> using namespace std; const int maxn = ...
- Jenkins自动化CI CD流水线之7--流水线自动化发布PHP项目
一.前提 环境为:lnmp PHP项目:wordpress(此处我们下载一个wordpress的源码.将其模拟为我们的代码上传到我们的git仓库) 二.配置 1)创建job 2)参数化构建 3)配置p ...
- 移动性能测试 | 持续集成中的 Android 稳定性测试
前言 谈到Android稳定测试,大多数会联想到使用monkey工具来做测试.google官方提供了monkey工具,可以很快速点击被应用,之前我有一篇帖子提到了monkey工具的使用,详见: htt ...
- shell read line
cat >b <<EOF line1 line2 line3 EOF # 方法1 while read line do echo ${line} done < <(cat ...
- my29_PXC集群状态查看
节点从集群中移除的状态 show status like '%wsrep%';wsrep_cluster_status为Disconnected则表示该节点已经不在集群中了,示例如下 > sho ...
- mgo03_linux7上安装mongo4.0
下载地址https://www.mongodb.com/download-center#community tar -xvf mongodb-linux-x86_64-rhel70-4.0.0.tgz ...
- 如何用Rational rose创建类图
UML中各种图形概要: 图名 对照 说明 用例图 use case diagram 用例图表明系统做什么,与谁交互.用例是系统提供的功能,参与者是系统与谁交互,参与者可以是人.系统或其他实体.一个系统 ...
- SQL 优化通用方法
1. 尽量避免用sub-queres, 可以采用join代替 2. exists代替in not exists 和not in 这两个的性能值得深究,应该不是差太多 3. 索引优化 4. 一些操作会导 ...