g++到底怎么装
时间:2010-08-06
来源:互联网
今天测了一段java代码,用SUN的VM直接运行:
基本上在30W那个数
多次测试在18万吧
上面的代码用了String unicode 牵涉到转码,所以我搞了个用byte测试的:
在四十三万左右
附上Code:
嗯,首先我们大概明白了这个String类转码的效率
String类版本的话 randName为:
自然run()函数也做相应调整
附注:这本是测试java.nio包的channel的效率的,还是String那个版本的时候nio这玩意儿比直接用stream快0.5倍
不过我确实手滑了。。
代码:
$ java -classpath bin/ Test |wc -l
327153
327153
基本上在30W那个数
代码:
$ ./Test.gcj |wc -l
174382
174382
多次测试在18万吧
上面的代码用了String unicode 牵涉到转码,所以我搞了个用byte测试的:
代码:
java -classpath bin/ TestForNIO |wc -l
626144
626144
代码:
./TestForNIO.gcj |wc -l
427479
427479
在四十三万左右
附上Code:
代码:
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.util.Random;
public class TestForNIO {
private static final class Queue extends Thread {
static final byte[] QB = "Queue:".getBytes();
static final byte LF = '\n';
boolean running = true;
ByteBuffer b = ByteBuffer.allocate(30);
public Queue() {
setDaemon(false);
start();
}
@Override
public void run() {
byte[] s;
while (running) {
b.put(QB);
s = randName();
b.put(s);
b.put(LF);
b.flip();
try {
stdout.write(b);
} catch (IOException e) {
}
b.clear();
}
}
}
private static WritableByteChannel stdout = Channels.newChannel(System.out);
private static Random randm = new Random();
/**
* The max length of the name defaults: 15
*/
public static int RAND_NAME_MAX_LENGTH = 15;
public static void main(String[] args) {
Queue t1 = new Queue();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
t1.running = false;
}
/**
* @return a name length vary from RAND_NAME_MAX_LENGTH
*/
public static byte[] randName() {
int length = randm.nextInt(RAND_NAME_MAX_LENGTH) + 1;
int i = 0;
byte[] s = new byte[length];
s[0] = (byte) ('A' + randm.nextInt(26));
for (; i < length; ++i) {
s[i] = ((byte) ('a' + randm.nextInt(26)));
}
return s;
}
}
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.util.Random;
public class TestForNIO {
private static final class Queue extends Thread {
static final byte[] QB = "Queue:".getBytes();
static final byte LF = '\n';
boolean running = true;
ByteBuffer b = ByteBuffer.allocate(30);
public Queue() {
setDaemon(false);
start();
}
@Override
public void run() {
byte[] s;
while (running) {
b.put(QB);
s = randName();
b.put(s);
b.put(LF);
b.flip();
try {
stdout.write(b);
} catch (IOException e) {
}
b.clear();
}
}
}
private static WritableByteChannel stdout = Channels.newChannel(System.out);
private static Random randm = new Random();
/**
* The max length of the name defaults: 15
*/
public static int RAND_NAME_MAX_LENGTH = 15;
public static void main(String[] args) {
Queue t1 = new Queue();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
t1.running = false;
}
/**
* @return a name length vary from RAND_NAME_MAX_LENGTH
*/
public static byte[] randName() {
int length = randm.nextInt(RAND_NAME_MAX_LENGTH) + 1;
int i = 0;
byte[] s = new byte[length];
s[0] = (byte) ('A' + randm.nextInt(26));
for (; i < length; ++i) {
s[i] = ((byte) ('a' + randm.nextInt(26)));
}
return s;
}
}
嗯,首先我们大概明白了这个String类转码的效率
String类版本的话 randName为:
代码:
/**
* @return a name length vary from RAND_NAME_MAX_LENGTH
*/
public static String randName() {
int length = randm.nextInt(RAND_NAME_MAX_LENGTH) + 1;
int i = 0;
StringBuffer s = new StringBuffer(length);
s.append((char) ('A'+randm.nextInt(26)));
for(; i < length; ++i) {
s.append((char) ('a'+randm.nextInt(26)));
}
return s.toString();
}
* @return a name length vary from RAND_NAME_MAX_LENGTH
*/
public static String randName() {
int length = randm.nextInt(RAND_NAME_MAX_LENGTH) + 1;
int i = 0;
StringBuffer s = new StringBuffer(length);
s.append((char) ('A'+randm.nextInt(26)));
for(; i < length; ++i) {
s.append((char) ('a'+randm.nextInt(26)));
}
return s.toString();
}
自然run()函数也做相应调整
附注:这本是测试java.nio包的channel的效率的,还是String那个版本的时候nio这玩意儿比直接用stream快0.5倍
不过我确实手滑了。。
作者: zhu527812567 发布时间: 2010-08-06
完全不懂java
只写 c/c++ shell python
只写 c/c++ shell python
作者: fanhe 发布时间: 2010-08-10
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28