Last active
November 12, 2019 05:51
-
-
Save cosminpopescu14/148657baf7bd973812312434305500cf to your computer and use it in GitHub Desktop.
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
//this performs horribly. It's a programming error in the file :) | |
package com.company; | |
import java.io.IOException; | |
import java.nio.charset.Charset; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Files; | |
import java.nio.file.OpenOption; | |
import java.nio.file.Paths; | |
import static java.nio.file.StandardOpenOption.APPEND; | |
public class Bench { | |
public static void main(String[] args) throws IOException { | |
var start = System.currentTimeMillis(); | |
parse(); | |
var took = (System.currentTimeMillis() - start) / 1000f; | |
System.out.println(took); | |
} | |
private static void parse() throws IOException { | |
var path = Paths.get("C:\\Users\\cosmi\\Desktop\\random_numbers.txt"); | |
var charSet = StandardCharsets.UTF_8; | |
try (var reader = Files.newBufferedReader(path, charSet)) { | |
String line; | |
while ((line = reader.readLine()) != null) { | |
var x = Double.parseDouble(line.split("\t")[0]); | |
var y = Double.parseDouble(line.split("\t")[1]); | |
double res = x / y; | |
writeResult(x, y, res); | |
} | |
} | |
} | |
private static void writeResult( double x, double y, double res) throws IOException { | |
var resultFile = "C:\\Users\\cosmi\\Desktop\\results_contest_devforum.txt"; | |
var charSet = StandardCharsets.UTF_8; | |
try (var writer = Files.newBufferedWriter(Paths.get(resultFile), charSet, APPEND)) { | |
writer.write(x + "\t" + y + "\t" + res + "\n"); | |
} | |
} | |
} |
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
package com.company; | |
import java.io.IOException; | |
import java.lang.management.ManagementFactory; | |
import java.nio.CharBuffer; | |
import java.nio.MappedByteBuffer; | |
import java.nio.channels.FileChannel; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardOpenOption; | |
import java.util.EnumSet; | |
public class Bench { | |
public static void main(String[] args) throws IOException { | |
var start = System.currentTimeMillis(); | |
parse(); | |
var took = (System.currentTimeMillis() - start) / 1000f; | |
System.out.println(took); | |
System.out.println("Used memory: " + ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed() / 1024 / 1024); | |
} | |
private static void parse() throws IOException { | |
var path = Paths.get("C:\\Users\\cosmi\\Desktop\\random_numbers.txt"); | |
var charSet = StandardCharsets.UTF_8; | |
var sb = new StringBuilder(); | |
try (var reader = Files.newBufferedReader(path, charSet)) { | |
String line; | |
while ((line = reader.readLine()) != null) { | |
var x = Double.parseDouble(line.split("\t")[0]); | |
var y = Double.parseDouble(line.split("\t")[1]); | |
double res = x / y; | |
sb.append(x).append("\t").append(y).append("\t").append(res).append("\n"); //desi lumea zice sa nu folosesti sb | |
} | |
} | |
writeResult(sb); | |
} | |
private static void writeResult(StringBuilder data) throws IOException { | |
var data1 = CharBuffer.wrap(data); | |
var pathToWrite = Paths.get("C:\\Users\\cosmi\\Desktop\\results_contest_devforum.txt"); | |
try (FileChannel fileChannel = (FileChannel) Files | |
.newByteChannel(pathToWrite, EnumSet.of( | |
StandardOpenOption.READ, | |
StandardOpenOption.WRITE, | |
StandardOpenOption.TRUNCATE_EXISTING))) { | |
MappedByteBuffer mappedByteBuffer = fileChannel | |
.map(FileChannel.MapMode.READ_WRITE, 0, data1.length()); | |
if (mappedByteBuffer != null) { | |
mappedByteBuffer.put( | |
StandardCharsets.UTF_8.encode(data1)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment