Last active
January 28, 2019 14:39
-
-
Save ckpiyanon/b27268541d5bed97ea29a7c1de0b05aa to your computer and use it in GitHub Desktop.
baseline translator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.math.BigDecimal; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import java.util.Random; | |
import java.util.TreeMap; | |
public class Main { | |
static String read3CharsFromBuffer(BufferedReader bfr) throws Exception { | |
StringBuilder sb = new StringBuilder(); | |
for(int i = 0;i < 3;i++) { | |
int ch = bfr.read(); | |
if(ch >= 0) sb.append((char)ch); | |
else return null; | |
} | |
return sb.toString(); | |
} | |
static void generateInputs(final int NUM_INPUT,final int SIZE) throws Exception { | |
final char[] ARR = {'A','U','G','C'}; | |
Random rand = new Random(); | |
BufferedWriter bfw; | |
for(int i = 0;i < NUM_INPUT;i++) { | |
bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(String.format("in%03d.txt",i + 1)))); | |
System.out.println("Generating file " + (i + 1) + " out of " + NUM_INPUT); | |
for(int k = 0;k < SIZE;k++) | |
bfw.append(ARR[rand.nextInt(4)]); | |
bfw.flush(); | |
bfw.close(); | |
System.out.println("Done generating file " + (i + 1) + " out of " + NUM_INPUT); | |
} | |
} | |
static void process_map(final int NUM_INPUT,final int SIZE,final Map<String,String> map) throws Exception { | |
BufferedReader bfr; | |
BufferedWriter bfw; | |
long time; | |
BigDecimal overallTime = BigDecimal.ZERO; | |
BigDecimal minTime = BigDecimal.TEN.pow(20); | |
BigDecimal maxTime = BigDecimal.ZERO; | |
for(int i = 0;i < NUM_INPUT;i++) { | |
time = System.currentTimeMillis(); | |
bfr = new BufferedReader(new InputStreamReader(new FileInputStream(String.format("in%03d.txt",i + 1)))); | |
bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(String.format("out%03d.txt",i + 1)))); | |
// System.out.println("Processing file " + (i + 1) + " out of " + NUM_INPUT); | |
String tmp; | |
while((tmp = read3CharsFromBuffer(bfr)) != null) { | |
bfw.append(map.get(tmp).charAt(0)); | |
} | |
bfr.close(); | |
bfw.flush(); | |
bfw.close(); | |
// System.out.println("Done processing file " + (i + 1) + " out of " + NUM_INPUT); | |
BigDecimal tmpTime = BigDecimal.valueOf((System.currentTimeMillis() - time) / 1000.0); | |
overallTime = overallTime.add(tmpTime); | |
minTime = minTime.min(tmpTime); | |
maxTime = maxTime.max(tmpTime); | |
} | |
System.out.println("Data structure: " + map.getClass().getSimpleName()); | |
System.out.println("Average time used: " + overallTime.divide(BigDecimal.valueOf(NUM_INPUT)) + " seconds"); | |
System.out.println("Min time used: " + minTime + " seconds"); | |
System.out.println("Max time used: " + maxTime + " seconds"); | |
} | |
static void process_array(final int NUM_INPUT,final int SIZE,final Map<String,String> map) throws Exception { | |
BufferedReader bfr; | |
BufferedWriter bfw; | |
int charMap[] = new int[26]; | |
charMap['A' - 'A'] = 0; | |
charMap['U' - 'A'] = 1; | |
charMap['G' - 'A'] = 2; | |
charMap['C' - 'A'] = 3; | |
String mapper[][][] = new String[4][4][4]; | |
long time; | |
BigDecimal overallTime = BigDecimal.ZERO; | |
BigDecimal minTime = BigDecimal.TEN.pow(20); | |
BigDecimal maxTime = BigDecimal.ZERO; | |
for(Entry<String,String> entry:map.entrySet()) { | |
String key = entry.getKey(); | |
mapper[charMap[key.charAt(0) - 'A']][charMap[key.charAt(1) - 'A']][charMap[key.charAt(2) - 'A']] = entry.getValue(); | |
} | |
for(int i = 0;i < NUM_INPUT;i++) { | |
time = System.currentTimeMillis(); | |
bfr = new BufferedReader(new InputStreamReader(new FileInputStream(String.format("in%03d.txt",i + 1)))); | |
bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(String.format("out%03d.txt",i + 1)))); | |
// System.out.println("Processing file " + (i + 1) + " out of " + NUM_INPUT); | |
String tmp; | |
while((tmp = read3CharsFromBuffer(bfr)) != null) { | |
bfw.append(mapper[charMap[tmp.charAt(0) - 'A']][charMap[tmp.charAt(1) - 'A']][charMap[tmp.charAt(2) - 'A']]); | |
} | |
bfr.close(); | |
bfw.flush(); | |
bfw.close(); | |
// System.out.println("Done processing file " + (i + 1) + " out of " + NUM_INPUT); | |
BigDecimal tmpTime = BigDecimal.valueOf((System.currentTimeMillis() - time) / 1000.0); | |
overallTime = overallTime.add(tmpTime); | |
minTime = minTime.min(tmpTime); | |
maxTime = maxTime.max(tmpTime); | |
} | |
System.out.println("Data structure: " + charMap.getClass().getSimpleName()); | |
System.out.println("Average time used: " + overallTime.divide(BigDecimal.valueOf(NUM_INPUT)) + " seconds"); | |
System.out.println("Min time used: " + minTime + " seconds"); | |
System.out.println("Max time used: " + maxTime + " seconds"); | |
} | |
public static void main(String args[]) throws Exception { | |
final int SIZE = 1000000000; | |
final int NUM_INPUT = 10; | |
generateInputs(NUM_INPUT,SIZE); | |
Map<String,String> map; | |
map = new HashMap<String,String>(); | |
map.put("UUU","F"); map.put("UUC","F"); map.put("UUA","L"); map.put("UUG","L"); map.put("UCU","S"); map.put("UCC","S"); map.put("UCA","S"); map.put("UCG","S"); | |
map.put("UAU","Y"); map.put("UAC","Y"); map.put("UAA"," "); map.put("UAG"," "); map.put("UGU","C"); map.put("UGC","C"); map.put("UGA"," "); map.put("UGG","W"); | |
map.put("CUU","L"); map.put("CUC","L"); map.put("CUA","L"); map.put("CUG","L"); map.put("CCU","P"); map.put("CCC","P"); map.put("CCA","P"); map.put("CCG","P"); | |
map.put("CAU","H"); map.put("CAC","H"); map.put("CAA","Q"); map.put("CAG","Q"); map.put("CGU","R"); map.put("CGC","R"); map.put("CGA","R"); map.put("CGG","R"); | |
map.put("AUU","I"); map.put("AUC","I"); map.put("AUA","I"); map.put("AUG","M"); map.put("ACU","T"); map.put("ACC","T"); map.put("ACA","T"); map.put("ACG","T"); | |
map.put("AAU","N"); map.put("AAC","N"); map.put("AAA","K"); map.put("AAG","K"); map.put("AGU","S"); map.put("AGC","S"); map.put("AGA","R"); map.put("AGG","R"); | |
map.put("GUU","V"); map.put("GUC","V"); map.put("GUA","V"); map.put("GUG","V"); map.put("GCU","A"); map.put("GCC","A"); map.put("GCA","A"); map.put("GCG","A"); | |
map.put("GAU","D"); map.put("GAC","D"); map.put("GAA","E"); map.put("GAG","E"); map.put("GGU","G"); map.put("GGC","G"); map.put("GGA","G"); map.put("GGG","G"); | |
process_map(NUM_INPUT,SIZE,map); | |
map = new TreeMap<String,String>(map); | |
process_map(NUM_INPUT,SIZE,map); | |
process_array(NUM_INPUT,SIZE,map); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment