과 비교하여 왜 느린 지 최근에 우리는 100,000 개 이상의 xml 파일이 있고 그 중 모두가 xml의 특정 데이터 수정이 필요한 요구 사항이있었습니다. 간단한 perl 명령은 작업을 수행하지만 파일이 위치한 시스템에는 perl이 설치되어 있지 않습니다. 따라서 나는 작은 C# 코드를 작성하여 작업을 수행했습니다.이 C++ 코드가 C# 변종
private static void ModifyXML(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
string path = @args[0];
string opath = @args[1];
string token = "value_date=\"20121130\"";
string target = "value_date=\"20121019\"";
Parallel.ForEach(Directory.EnumerateFiles(path), (file) =>
{
StringBuilder sb = new StringBuilder(File.ReadAllText(file));
sb.Remove(0, 55);
sb.Replace(token, target);
var filename = file.Split(new char[] { '\\' }).Last();
File.WriteAllText(string.Format("{0}\\{1}", opath, filename), sb.ToString());
});
TimeSpan ts = sw.Elapsed;
Console.WriteLine("Took {0} secs", ts.TotalSeconds);
}
C++ 버전을 구현하기로 결정했습니다. C++ 버전이 C# 버전보다 훨씬 빠르지 않은 것으로 나타났습니다. In은 두 버전 모두 sevaral times을 실행했습니다. 사실, 실행 중 일부는 C# 버전만큼 빠릅니다.
C#의 경우 .NET 4.0을 사용하고 C++의 경우 VC10을 사용합니다. 사실, 사람들이 빠르고 너무 적은 모호한 버그 코딩 끝 - 일부 사람들의 인식에도 불구하고
void FileHandling(std::string src, std::string dest)
{
namespace fs = boost::filesystem;
auto start = boost::chrono::system_clock::now();
string token = "value_date=\"20121130\"";
string target = "value_date=\"20121019\"";
fs::directory_iterator end_iter;
fs::directory_iterator dir_itr(src);
vector<fs::path> files;
files.insert(files.end(), dir_itr, end_iter);
string dest_path = dest + "\\";
parallel_for_each(files.begin(), files.end(), [=](const fs::path& filepath)
{
ifstream inpfile (filepath.generic_string());
string line;
line.insert(line.end(), istreambuf_iterator<char>(inpfile), istreambuf_iterator<char>());
line.erase(0, 55);
auto index = line.find(token, 0);
if (index != string::npos)
{
line.replace(index, token.size(), target);
}
ofstream outfile(dest_path + filepath.filename().generic_string());
outfile << line;
});
boost::chrono::duration<double> finish = boost::chrono::system_clock::now() - start;
std::cout << "Took " << finish.count() << " secs\n";
}
왜 다른 코드를 기대합니까? –
어떻게 C++ 코드를 컴파일 했습니까? – jogojapan
@jogojapan, 출시 빌드. – Jagannath