写在前边的实现需求:

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提高数据导入效率优化思路》的更多相关文章

  1. 简单物联网:外网访问内网路由器下树莓派Flask服务器

    最近做一个小东西,大概过程就是想在教室,宿舍控制实验室的一些设备. 已经在树莓上搭了一个轻量的flask服务器,在实验室的路由器下,任何设备都是可以访问的:但是有一些限制条件,比如我想在宿舍控制我种花 ...

  2. 利用ssh反向代理以及autossh实现从外网连接内网服务器

    前言 最近遇到这样一个问题,我在实验室架设了一台服务器,给师弟或者小伙伴练习Linux用,然后平时在实验室这边直接连接是没有问题的,都是内网嘛.但是回到宿舍问题出来了,使用校园网的童鞋还是能连接上,使 ...

  3. 外网访问内网Docker容器

    外网访问内网Docker容器 本地安装了Docker容器,只能在局域网内访问,怎样从外网也能访问本地Docker容器? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Docker容器 ...

  4. 外网访问内网SpringBoot

    外网访问内网SpringBoot 本地安装了SpringBoot,只能在局域网内访问,怎样从外网也能访问本地SpringBoot? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装Java 1 ...

  5. 外网访问内网Elasticsearch WEB

    外网访问内网Elasticsearch WEB 本地安装了Elasticsearch,只能在局域网内访问其WEB,怎样从外网也能访问本地Elasticsearch? 本文将介绍具体的实现步骤. 1. ...

  6. 怎样从外网访问内网Rails

    外网访问内网Rails 本地安装了Rails,只能在局域网内访问,怎样从外网也能访问本地Rails? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Rails 默认安装的Rails端口 ...

  7. 怎样从外网访问内网Memcached数据库

    外网访问内网Memcached数据库 本地安装了Memcached数据库,只能在局域网内访问,怎样从外网也能访问本地Memcached数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装 ...

  8. 怎样从外网访问内网CouchDB数据库

    外网访问内网CouchDB数据库 本地安装了CouchDB数据库,只能在局域网内访问,怎样从外网也能访问本地CouchDB数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Cou ...

  9. 怎样从外网访问内网DB2数据库

    外网访问内网DB2数据库 本地安装了DB2数据库,只能在局域网内访问,怎样从外网也能访问本地DB2数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动DB2数据库 默认安装的DB2 ...

  10. 怎样从外网访问内网OpenLDAP数据库

    外网访问内网OpenLDAP数据库 本地安装了OpenLDAP数据库,只能在局域网内访问,怎样从外网也能访问本地OpenLDAP数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动 ...

随机推荐

  1. 从100PV到1亿级PV网站架构演变(转)

    http://www.linuxde.net/2013/05/13581.html 一个网站就像一个人,存在一个从小到大的过程.养一个网站和养一个人一样,不同时期需要不同的方法,不同的方法下有共同的原 ...

  2. matplotlib类

    1.plt.subplot 在matplotlib下,一个Figure对象可以包含多个子图(Axes),可以使用subplot()快速绘制,其调用形式如下:subplot(numRows, numCo ...

  3. Gym - 101845F 最大流

    The UN finals are here!, the coaches/ex-coaches team is creating a new exciting contest to select wh ...

  4. 5.Min Stack(能返回最小数的栈)

    Level:   Easy 题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element ...

  5. 【转】JavaScript里Function函数实现可变参数

    转载:  http://www.oschina.net/question/54100_15938 使用javascript类库函数时,经常会遇到一个函数,可以使用不同个数的参数的情况 比如:exp(v ...

  6. Unity组件

    在学习C++的时候,对于面对对象有点了解.然后也使用过一段时间的Unity,用起来还是觉得,怎么这么好用.耦合性极低.当时不知道这是基于组件编程.所以现在来学习下基于组件的知识,并比较下基于组件和基于 ...

  7. sqlserver 服务器监控

    1.表锁 查看被锁的表:select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from ...

  8. my.常用的话

    1. 60普通副本+++ 60普通副本+++ 60普通副本+++ 50封妖+++50封妖+++50封妖+++ 60一本十妖+++ 60一本十妖+++ 60一本十妖+++ 60封妖+++60封妖+++6 ...

  9. requirej入门(二)

    requirejs中的一些基本知识,包括API使用方式等. 基本API require会定义三个变量:define,require,requirejs,其中require === requirejs, ...

  10. python3与python2的区别 记录一波

    1.性能 Py3.0运行 pystone benchmark的速度比Py2.5慢30%.Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可 以取得很好的优化结果. Py3.1性能比Py2 ...