我有一个令人费解的情况,我需要专家意见来解释下面解释的现象的原因。几周前,我举办了一场名为“面向 Java 开发人员的 .NET 概述”的 session ,作为其中的一部分,我编写了一个快速类 C#(3.5 框架)来逐行读取一个文件并写入另一个文件(在迭代中)。由于我的听众是 java 开发人员,我在 java 类中有相同的代码以进行并排比较。然而,当我在同一台机器上运行这些类时,令我惊讶的是,Java 代码的运行速度一直是 C# 代码的两倍。我在 C# 代码中尝试了很多优化来缩小差距,但都没有成功。必须有一个解释,我正在寻找可以解释原因的人。我附上了这两个类的源代码供您引用。
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);
}
}
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");
}
}
最佳答案
一方面:在 Java 中,您使用的是平台的默认编码。这很可能是固定的“每个字符一个字节”编码,这显然比使用 UTF-8 更简单,而 .NET 默认使用 UTF-8。
此外,您在 .NET 中编写了两个换行符,而在 Java 中只编写了一个。
要检查的一件事是您是否受 CPU 限制或 IO 限制。我预计这是受 IO 限制的,但在此之前我确实感到很惊讶。
最后,您应该在重新启动后运行每个测试,以尝试尽可能地从等式中删除磁盘缓存。
关于c# - c# 和 java 之间的基准测试和差异原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1032430/