惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
IT之家
IT之家
V
V2EX
Jina AI
Jina AI
V
Visual Studio Blog
有赞技术团队
有赞技术团队
博客园 - 司徒正美
博客园 - 叶小钗
The Cloudflare Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
Google Online Security Blog
Google Online Security Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
N
News and Events Feed by Topic
N
News and Events Feed by Topic
The Last Watchdog
The Last Watchdog
W
WeLiveSecurity
月光博客
月光博客
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
SecWiki News
SecWiki News
博客园_首页
罗磊的独立博客
量子位
Latest news
Latest news
I
Intezer
V
Vulnerabilities – Threatpost
A
Arctic Wolf
Last Week in AI
Last Week in AI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
S
SegmentFault 最新的问题
S
Security Affairs
阮一峰的网络日志
阮一峰的网络日志
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
N
News | PayPal Newsroom

博客园 - 震

关于反射中方法指针中我遇到的问题 一些关于数据库的问题 使用ORACLE和MYSQL的简单心得 出来混总有一天要还的 Java中的访问修饰符 Java中的static、final、this、super xslt学习简单笔记 XML schema学习笔记 DOM中对象的方法(转贴) 学习JavaScript的开源好东东 关于CSS中的一些问题 - 震 - 博客园 web标准的悄然袭来 在JAVA中调用外部可执行程序 - 震 - 博客园 一小练习 - 震 - 博客园 Java中的String类和StringBuffer类 将Java对象转为String的几种方法剖析(转贴) java 中的随机数用法 两种特殊的Java容器类List和Set分析[转贴] java.util.list初体验
替换字符性能测试
· 2006-06-12 · via 博客园 - 震

import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.ArrayList;
public class Test610{
    
public static void main(String[] aegs){
        System.out.println(
"读出记录数需要的时间是"+getTime()+"毫秒");
         System.out.println(
"需要的时间是"+getTime('f','z')+"毫秒");
        System.out.println(
"需要的时间是"+getListTime('f','z')+"毫秒");    
    }

    
    
//计算出读取完所有属性文件里的记录时间消耗
    static long getTime(){
        
//s,e起始时间
        long s= System.currentTimeMillis();
        Properties pro 
=new Properties();
        
try{
            pro.load(
new FileInputStream("Test610.Properties"));
        }
catch(IOException e){
        }

        
        
//遍历整个属性文件
        for(int i =1;pro.getProperty("a"+i) != null;i++){
        System.out.println(pro.getProperty(
"a"+i));    
        }

        
        
long e =System.currentTimeMillis();
        
return (e-s); 
    }

    
    
//读取属性文件里的所有记录,并且查找替换,最后把替换后的记录写入新属性文件,并记录时间消耗
    static long getTime(char olds,char news){
        
        
long s= System.currentTimeMillis();
        Properties pro 
=new Properties();
        
try{
            pro.load(
new FileInputStream("Test610.Properties"));
        }
catch(IOException e){
        }

        
        
        
for(int i =1;pro.getProperty("a"+i) != null;i++){
            pro.setProperty(
"a"+i,pro.getProperty("a"+i).replace(olds,news));
        }
    
            
try{
                pro.store(
new FileOutputStream("Test610_new1.Properties"),null);
                }
catch(IOException e){
                }

            
        
    
        
long e =System.currentTimeMillis();
        
return (e-s);         
    }

    
    
//读取属性文件里的所有记录,并放到集合进行替换,把替换后的记录写入新属性文件,并记录时间消耗
    static long getListTime(char olds,char news){
        
long s= System.currentTimeMillis();
        Properties pro 
=new Properties();
        
try{
            pro.load(
new FileInputStream("Test610.Properties"));
        }
catch(IOException e){
        }

        
        List list 
= new ArrayList();
        
//List list = new java.util.LinkedList();
        
        
for(int i =1;pro.getProperty("a"+i) != null;i++){
            list.add(pro.getProperty(
"a"+i));    
        }

        
for(int i=1;i<=list.size();i++){
            pro.setProperty((
"a"+i),list.get(i-1).toString().replace(olds,news));
        }
    
                
try{
                pro.store(
new FileOutputStream("Test610_new2.Properties"),null);
                }
catch(IOException e){
                }

            

    
        
long e =System.currentTimeMillis();
        
return (e-s); 
        }

}

经过测试把数据放到集合替换比直接字符串替换效率要高。
PS:再贴个产生数据行的用列

import java.util.Random;
import java.io.IOException;
import java.io.FileOutputStream;
import java.util.Properties;
import java.lang.StringBuffer;
public class Test610add{
    
public static void main(String[] args){
        Properties pro 
=new Properties();    
        Random ran 
=new Random();
        StringBuffer str 
= new StringBuffer();
        
        
for(int i=1;i<=200;i++){
            str.delete(
0,5);
            
for(int k=0;k<5;k++){
                str.append(String.valueOf((
char)(ran.nextInt(26)+97)));
            }

        pro.setProperty((
"a"+i),str.toString());
    
        }

        
try{
            pro.store(
new FileOutputStream("Test610.Properties"),null);
            }
catch(IOException e){
            }

        
    }

}

产生200行的数据.