Tengo una situación desconcertante y necesitaría una opinión experta sobre la causa del fenómeno que se explica a continuación. Hace un par de semanas, realicé una sesión titulada "Una visión general de .NET para desarrolladores de Java" y como parte de ella escribí una clase rápida C# (3.5 framework) para leer desde un archivo y escribir en otro archivo línea por línea (en una iteración). Como mi público era desarrollador de Java, tenía el mismo código en una clase de Java para la comparación de lado a lado. Sin embargo, cuando ejecuté estas clases en la misma máquina, para mi sorpresa, el código java se ejecutó dos veces más rápido que el código C#. He intentado muchas optimizaciones en el código C# para reducir la brecha, pero no pude tener éxito. Tiene que haber una explicación y estoy buscando a alguien que pueda explicar la causa. Adjunto el código fuente de ambas clases para su referencia.punto de referencia y la causa de la diferencia entre C# y java
clase Java
public class ReadWriteTextFile {
static public String getContents(File aFile, String OutPutFileName) {
StringBuilder contents = new StringBuilder();
try {
BufferedReader input = new BufferedReader(new FileReader(aFile));
FileReader x = new FileReader(aFile);
try {
String line = null;
while ((line = input.readLine()) != null){
setContents(OutPutFileName, line + System.getProperty("line.separator"));
}
}
finally {
input.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}
return contents.toString();
}
static public void setContents(String FileName, String aContents)
throws FileNotFoundException, IOException {
try {
FileWriter fstream = new FileWriter(FileName, true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(aContents);
out.close();
} catch (Exception xe) {
xe.printStackTrace();
}
}
public static void main (String[] aArguments) throws IOException {
System.out.println(getDateTime() + ": Started");
File testFile = new File("C:\\temp\\blah.txt");
String testFile2 = "C:\\temp\\blahblah.txt";
for(int i=0; i<100; i++){
getContents(testFile, testFile2);
}
System.out.println(getDateTime() + ": Ended");
}
private synchronized static String getDateTime() {
DateFormat dateFormat = new SimpleDateFormat(
"yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
}
clase C#
class ReadWriteTextFile
{
static void Main(string[] args)
{
System.Diagnostics.Trace.WriteLine(getDateTime() + ": Started");
String testFile = "C:\\temp\\blah.txt";
String testFile2 = "C:\\temp\\blahblah.txt";
for(int i=0; i<100; i++){
getContents(testFile, testFile2);
}
System.Diagnostics.Trace.WriteLine(getDateTime() + ": Ended");
}
static public void getContents(String sourceFile, String targetFile) {
try {
using (StreamReader r = File.OpenText(sourceFile))
{
String line;
while ((line = r.ReadLine()) != null)
{
setContents(targetFile, line);
}
r.Close();
}
}
catch (IOException ex){
Console.WriteLine(ex.StackTrace);
}
}
static public void setContents(String targetFile, String aContents)
{
try {
//FileStream fsO = new FileStream(targetFile, FileMode.Append);
//StreamWriter w = new StreamWriter(fsO);
FileStream fs = new FileStream(targetFile, FileMode.Append,
FileAccess.Write, FileShare.None);
using (StreamWriter w = new StreamWriter(fs))
{
w.WriteLine(aContents + "\n");
}
} catch (Exception xe) {
Console.WriteLine(xe.StackTrace);
}
}
private static String getDateTime() {
DateTime dt = DateTime.Now;
return dt.ToString("yyyy/MM/dd HH:mm:ss");
}
}
Quizás es un caso simple de 'java puede hacer esto en particular más rápido'. – karim79
¿Qué JDK estás usando? ¿Alguna optimización activada? – duffymo
Intente volver a ejecutar la versión C# inmediatamente después de ejecutarlo. –