gpt4 book ai didi

java - 在Java中提取大写字符的最快方法

转载 作者:搜寻专家 更新时间:2023-10-30 19:55:18 25 4
gpt4 key购买 nike

我目前正在处理字符串形式的 DNA 序列,其中内含子为小写字符,外显子为大写字符。该方法的目的是尽快以字符串的形式检索外显子。

序列示例:

ATGGATGACAGgtgagaggacactcgggtcccagccccaggctctgccctcaggaagggggtcagctctcaggggcatctccctctcacagcccagccctggggatgatgtgggagccccatttatacacggtgcctccttctctcctagAGCCTACATAG

我的第一个版本使用的是 String replaceAll() 方法,但速度特别慢:

public String getExons(String sequence) {
return sequence.replaceAll("[atgc]", "");
}

所以我尝试了一个改进了性能但仍然相当慢的新版本:

public String getExons(String sequence) {
StringBuilder exonBuilder = new StringBuilder();

for (int i = 0; i < sequence.length(); i++) {
char c = sequence.charAt(i);
if (c == 'A' || c == 'T' || c == 'G' || c == 'C') exonBuilder.append(c);
}
return exonBuilder.toString();

是否有另一种方法可以提高性能?

最佳答案

您需要使用带有双指针技巧的字符数组。 我在我的机器上得到了这个结果:

编辑:更新了预热阶段。 Java 是来自 Ubuntu 14 LTS 的 OpenJDK 8

Edit2: 哈希表是迄今为止最快的。

Edit3:我的代码中有一个错误。双指针技巧是最快的。

GTCtgACgGT
getExons1: 1068
getExons2: 377
getExons3: 313
getExons3b: 251
getExons4: 586
getExons5: 189
getExons6: 671

Edit4:使用带有 1M DNA 字符串的 JMH 运行基准测试。结果与我之前关于“x 优于 y”的基准一致,最差的是正则表达式,最好的是双指针,第二好的是朴素的 3B:

Benchmark                  Mode  Cnt    Score   Error  Units
MyBenchmark.benchExons1 thrpt 200 33.659 ± 1.036 ops/s
MyBenchmark.benchExons2 thrpt 200 107.095 ± 4.074 ops/s
MyBenchmark.benchExons3a thrpt 200 118.543 ± 3.779 ops/s
MyBenchmark.benchExons3b thrpt 200 163.717 ± 4.602 ops/s
MyBenchmark.benchExons4 thrpt 200 69.942 ± 2.019 ops/s
MyBenchmark.benchExons5 thrpt 200 191.142 ± 5.307 ops/s
MyBenchmark.benchExons6 thrpt 200 57.654 ± 1.963 ops/s

Edit5:使用 10 MB 字符串:

Benchmark                  Mode  Cnt   Score   Error  Units
MyBenchmark.benchExons1 thrpt 200 4.640 ± 0.068 ops/s
MyBenchmark.benchExons2 thrpt 200 13.451 ± 0.161 ops/s
MyBenchmark.benchExons3a thrpt 200 15.379 ± 0.232 ops/s
MyBenchmark.benchExons3b thrpt 200 19.550 ± 0.181 ops/s
MyBenchmark.benchExons4 thrpt 200 8.510 ± 0.147 ops/s
MyBenchmark.benchExons5 thrpt 200 24.343 ± 0.331 ops/s
MyBenchmark.benchExons6 thrpt 200 7.339 ± 0.074 ops/s

代码:

package org.sample;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;

import java.util.HashMap;
import java.util.Random;

@State(Scope.Thread)
public class MyBenchmark {

String DNA;

public MyBenchmark() {
DNA = buildRandomDNA(1000000);
}

static String letters = "ATGCatgc";

public static String buildRandomDNA(int size) {
StringBuilder builder = new StringBuilder(size);
Random r = new Random();

for (int i = 0; i < size; ++i) {
builder.append(letters.charAt(r.nextInt(letters.length())));
}

return builder.toString();
}

@Benchmark
public void benchExons1() {
getExons1(DNA);
}

@Benchmark
public void benchExons2() {
getExons2(DNA);
}

@Benchmark
public void benchExons3a() {
getExons3a(DNA);
}

@Benchmark
public void benchExons3b() {
getExons3b(DNA);
}

@Benchmark
public void benchExons4() {
getExons4(DNA);
}

@Benchmark
public void benchExons5() {
getExons5(DNA);
}

@Benchmark
public void benchExons6() {
getExons6(DNA);
}

public static String getExons1(String sequence) {
return sequence.replaceAll("[atgc]", "");
}

public static String getExons2(String sequence) {
StringBuilder exonBuilder = new StringBuilder();

for (int i = 0; i < sequence.length(); i++) {
char c = sequence.charAt(i);
if (c == 'A' || c == 'T' || c == 'G' || c == 'C')
exonBuilder.append(c);
}
return exonBuilder.toString();
}

public static String getExons3a(String sequence) {
StringBuilder exonBuilder = new StringBuilder();

for (int i = 0; i < sequence.length(); i++) {
char c = sequence.charAt(i);
if (c <= 'Z') {
exonBuilder.append((char) c);
}
}

return exonBuilder.toString();
}

public static String getExons3b(String sequence1) {
char[] sequence = sequence1.toCharArray();
StringBuilder exonBuilder = new StringBuilder();

for (int i = 0; i < sequence.length; i++) {
if (sequence[i] <= 'Z') {
exonBuilder.append(sequence[i]);
}
}

return exonBuilder.toString();
}

public static HashMap<String, String> M = new HashMap<String, String>();

public static void buildTable() {
for (int a = 0; a < letters.length(); ++a) {
for (int b = 0; b < letters.length(); ++b) {
for (int c = 0; c < letters.length(); ++c) {
for (int d = 0; d < letters.length(); ++d) {
String key = "" + letters.charAt(a) + letters.charAt(b) + letters.charAt(c) + letters.charAt(d);
M.put(key, getExons1(key));
}
}
}
}
}

public static String getExons4(String sequence1) {
char[] sequence = sequence1.toCharArray();
StringBuilder exonBuilder = new StringBuilder();

for (int i = 0; i < sequence.length; i += 4) {
exonBuilder.append(M.get(new String(sequence, i, 4)));
}

return exonBuilder.toString();
}

public static String getExons5(String sequence1) {
char[] sequence = sequence1.toCharArray();
int p = 0;

for (int i = 0; i < sequence.length; i++) {
if (sequence[i] <= 'Z') {
sequence[p] = sequence[i];
++p;
}
}

return new String(sequence, 0, p);
}

public static int dnatoint(char[] s, int start, int len) {
int key = 0;
for (; len > 0; len--, start++) {
switch (s[start]) {
case 'A': key = (key << 3) | 0; break;
case 'C': key = (key << 3) | 1; break;
case 'G': key = (key << 3) | 2; break;
case 'T': key = (key << 3) | 3; break;
case 'a': key = (key << 3) | 4; break;
case 'c': key = (key << 3) | 5; break;
case 'g': key = (key << 3) | 6; break;
case 't': key = (key << 3) | 7; break;
}
}
return key;
}

public static String[] M2 = new String[8*8*8*8];

public static void buildTable2() {
for (int a = 0; a < letters.length(); ++a) {
for (int b = 0; b < letters.length(); ++b) {
for (int c = 0; c < letters.length(); ++c) {
for (int d = 0; d < letters.length(); ++d) {
String key = "" + letters.charAt(a) + letters.charAt(b) + letters.charAt(c) + letters.charAt(d);
M2[dnatoint(key.toCharArray(), 0, 4)] = getExons1(key);
}
}
}
}
}

public static String getExons6(String sequence1) {
char[] sequence = sequence1.toCharArray();
StringBuilder exonBuilder = new StringBuilder();

assert (sequence.length % 4) == 0;

for (int i = 0; i < sequence.length; i += 4) {
exonBuilder.append(M2[dnatoint(sequence, i, 4)]);
}

return exonBuilder.toString();
}

static {
buildTable();
buildTable2();
}

//@Benchmark
public void testMethod() {
// This is a demo/sample template for building your JMH benchmarks. Edit as needed.
// Put your benchmark code here.
}

}

关于java - 在Java中提取大写字符的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39291251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com