ファイルに何百万ものレコードがあり、いくつかの計算を行う必要があります。このために、私はJavaプログラムとC ++プログラムの同じコピーを持っていますが、JavaはC ++よりもはるかに高速に実行されます。私が c++ に切り替えた主な理由は、マルチスレッドを実行してプログラムをより高速に実行することです。しかし、Java と C++ の間で 1 つのスレッド作業を比較すると、Java は半分の時間で作業を行います。
この問題を解決する必要があります。C++ はより高速になると思われますが、パフォーマンスが低下します。
いくつかの良い良いヘッドアップがあればいいので、調査して修正してみることができます.
ありがとう
これは、カンマ区切りのデータからオブジェクトを作成するクラスです
//Parser.cpp
#include "Parser.h"
#include "PriceBar.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <stdlib.h>
using namespace std;
vector<PriceBar> Parser :: parseFile(string file){
string STRING;
vector<PriceBar> bars;
ifstream infile;
infile.open (file.c_str());
int a=0;
string token;
while(getline(infile,STRING)) // To get you all the lines.
{
vector<string> data;
istringstream ss(STRING);
while(getline(ss, token, ',')) {
data.push_back(token);
}
//cout<<data[4]<<endl;
if(!data[1].empty()){
//cout << "if is working" << endl;
double open = atof(data[1].c_str());
double high = atof(data[2].c_str());
double low = atof(data[3].c_str());
double close = atof(data[4].c_str());
bars.push_back(PriceBar(open, high, low, close));
}//end of if
}//end of while
infile.close();
//cout << "parser is done " << bars[2].getOpen() <<endl;
//cout << bars.size() << endl;
return bars;
}
プライスバー クラス
/*
* PriceBar.cpp
*
* Created on: Nov 5, 2013
* Author: hansaka
*/
#include <iostream>
#include <string>
#include <vector>
#include "PriceBar.h"
using namespace std;
PriceBar :: PriceBar(double open, double high, double low, double close){
this -> open = open;
this -> high = high;
this -> low = low;
this -> close = close;
}
double PriceBar :: getOpen() {
return open;
}
void PriceBar :: setOpen(double open) {
this -> open = open;
}
double PriceBar :: getHigh() {
return high;
}
void PriceBar :: setHigh(double high) {
this -> high = high;
}
double PriceBar :: getLow() {
return low;
}
void PriceBar :: setLow(double low) {
this -> low = low;
}
double PriceBar :: getClose() {
return close;
}
void PriceBar :: setClose(double close) {
this -> close = close;
}
メインファイル
#include <iostream>
#include <vector>
#include <string>
#include "PriceBar.h"
#include "Parser.h"
#include <ctime>
using namespace std;
int main() {
Parser p;
//getting the counter ready
time_t tstart, tend;
//Starting the time
tstart = time(0);
vector<string> path;
path.push_back("file.csv");
for( vector<string>::const_iterator it = path.begin(); it != path.end(); ++it ){
// cout << *it << endl;
vector<PriceBar> priceBars = p.parseFile(*it);
//priceBars = p.parseFile(*it);
// cout << "done" << endl;
double maxHigh = 0.0;
double maxLow = 0.0;
double maxOpen = 0.0;
double maxClose = 0.0;
double maxVolume = 0.0;
double current = 0.0;
// cout << "hippy " << priceBars[2].getOpen() <<endl;
int size = priceBars.size();
// cout << "size = " << size << endl;
for (int j=0;j<size;j++) {
current = priceBars[j].getOpen();
if (current > maxOpen) {
maxOpen = current;
}
}//end of pricebar for
current = 0.0;
for (int j=0;j<size;j++) {
current = priceBars[j].getOpen();
if (current > maxHigh) {
maxHigh = current;
}
}
current = 0.0;
for (int j=0;j<size;j++) {
current = priceBars[j].getOpen();
if (current > maxLow) {
maxLow = current;
}
}
current = 0.0;
for (int j=0;j<size;j++) {
current = priceBars[j].getOpen();
if (current > maxClose) {
maxClose = current;
}
}
cout << "MaxHigh =" << maxOpen << " MaxLow = " << maxHigh
<< " MaxHigh =" << maxLow << " MaxLow = " << maxClose << endl;
}//end of it for
cout << "DONE" << endl;
//Ending the time count
tend = time(0);
cout << " It took " << difftime(tend, tstart) << " second(s).";
return 0;
}
私はこのコードを何度も編集しているため、コメントはあまりありません。参照用にコメントアウトしたコード部分があり、申し訳ありません。