提交 255c2d62 authored 作者: Thomas Mueller's avatar Thomas Mueller

Slightly improved performance.

上级 aa840b1e
......@@ -92,10 +92,11 @@ public class Data extends DataPage {
*/
public void writeInt(int x) {
byte[] buff = data;
buff[pos++] = (byte) (x >> 24);
buff[pos++] = (byte) (x >> 16);
buff[pos++] = (byte) (x >> 8);
buff[pos++] = (byte) x;
buff[pos] = (byte) (x >> 24);
buff[pos + 1] = (byte) (x >> 16);
buff[pos + 2] = (byte) (x >> 8);
buff[pos + 3] = (byte) x;
pos += 4;
}
/**
......@@ -106,7 +107,9 @@ public class Data extends DataPage {
*/
public int readInt() {
byte[] buff = data;
return (buff[pos++] << 24) + ((buff[pos++] & 0xff) << 16) + ((buff[pos++] & 0xff) << 8) + (buff[pos++] & 0xff);
int x = (buff[pos] << 24) + ((buff[pos+1] & 0xff) << 16) + ((buff[pos+2] & 0xff) << 8) + (buff[pos+3] & 0xff);
pos += 4;
return x;
}
/**
......@@ -911,26 +914,32 @@ public class Data extends DataPage {
* @return the value
*/
public int readVarInt() {
int b = data[pos++];
int b = data[pos];
if (b >= 0) {
pos++;
return b;
}
int x = b & 0x7f;
b = data[pos++];
b = data[pos + 1];
if (b >= 0) {
pos += 2;
return x | (b << 7);
}
x |= (b & 0x7f) << 7;
b = data[pos++];
b = data[pos + 2];
if (b >= 0) {
pos += 3;
return x | (b << 14);
}
x |= (b & 0x7f) << 14;
b = data[pos++];
b = data[pos + 3];
if (b >= 0) {
pos += 4;
return x | b << 21;
}
return x | ((b & 0x7f) << 21) | (data[pos++] << 28);
x |= ((b & 0x7f) << 21) | (data[pos + 4] << 28);
pos += 5;
return x;
}
/**
......
......@@ -201,10 +201,12 @@ public class ByteUtils {
* @param buff the byte array
*/
public static void clear(byte[] buff) {
for (int i = 0; buff != null && i < buff.length; i++) {
if (buff != null) {
for (int i = 0; i < buff.length; i++) {
buff[i] = 0;
}
}
}
/**
* Compare the contents of two byte arrays. If the content or length of the
......
......@@ -136,7 +136,7 @@ public class BenchA implements Bench {
int max = BenchA.DELTA;
// delta: -max .. +max
BigDecimal delta = new BigDecimal("" + (random.nextInt(max * 2) - max));
BigDecimal delta = BigDecimal.valueOf(random.nextInt(max * 2) - max);
long current = System.currentTimeMillis();
updateAccount.setBigDecimal(1, delta);
......
......@@ -11,7 +11,6 @@ import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import org.h2.constant.SysProperties;
import org.h2.message.Trace;
import org.h2.store.Data;
......@@ -47,7 +46,7 @@ import org.h2.value.ValueUuid;
*/
public class TestDataPage extends TestBase implements DataHandler {
private boolean testPerformance;
private boolean testPerformance = true;
/**
* Run just this test.
......@@ -59,10 +58,63 @@ public class TestDataPage extends TestBase implements DataHandler {
}
public void test() throws SQLException {
if (testPerformance) {
testPerformance();
System.exit(0);
return;
}
testValues();
testAll();
}
private void testPerformance() {
Data data = Data.create(null, 1024);
for (int j = 0; j < 4; j++) {
long time = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
data.reset();
for (int k = 0; k < 30; k++) {
data.writeString("Hello World");
}
}
// for (int i = 0; i < 5000000; i++) {
// data.reset();
// for (int k = 0; k < 100; k++) {
// data.writeInt(k * k);
// }
// }
// for (int i = 0; i < 200000; i++) {
// data.reset();
// for (int k = 0; k < 100; k++) {
// data.writeVarInt(k * k);
// }
// }
System.out.println("write: " + (System.currentTimeMillis() - time) + " ms");
}
for (int j = 0; j < 4; j++) {
long time = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
data.reset();
for (int k = 0; k < 30; k++) {
data.readString();
}
}
// for (int i = 0; i < 3000000; i++) {
// data.reset();
// for (int k = 0; k < 100; k++) {
// data.readVarInt();
// }
// }
// for (int i = 0; i < 50000000; i++) {
// data.reset();
// for (int k = 0; k < 100; k++) {
// data.readInt();
// }
// }
System.out.println("read: " + (System.currentTimeMillis() - time) + " ms");
}
}
private void testValues() throws SQLException {
testValue(ValueNull.INSTANCE);
testValue(ValueBoolean.get(false));
......@@ -150,10 +202,12 @@ public class TestDataPage extends TestBase implements DataHandler {
Data data = Data.create(null, 1024);
data.checkCapacity((int) v.getPrecision());
data.writeValue(v);
data.writeInt(123);
data.reset();
Value v2 = data.readValue();
assertEquals(v.getType(), v2.getType());
assertTrue(v.compareEqual(v2));
assertEquals(123, data.readInt());
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论