2
\$\begingroup\$

I have written a program to compare two 3D raw data equal or different. But I think the time consuming would be reduced if I use the STL. How to improve the program? The complete and varifiable code is showed as follows:

#include <cstdio>
#include <string>
#include <iostream>
#include <assert.h>
#include <cstdlib>
using namespace std;
int LoadSample(std::string path_sample, unsigned char*& raw_data, int* w_h_d) {
 FILE *fp;
 if ( fopen_s(&fp, path_sample.c_str(),"rb") ) 
 return 1; 
 int width = 0;
 int height = 0;
 int depth = 0;
 fread(&width,4,1,fp);
 fread(&height,4,1,fp);
 fread(&depth,4,1,fp);
 w_h_d[0] = width;
 w_h_d[1] = height;
 w_h_d[2] = depth;
 raw_data = (unsigned char*)malloc(width*height*depth*sizeof(unsigned char));
 fread(raw_data,height*width*depth,1,fp);
 fclose(fp);
 return 0;
}
int CompareData(unsigned char*& data_train, int* train_w_h_d, unsigned char*& data_test, int* test_w_h_d) {
 if (train_w_h_d[0] == test_w_h_d[0] && train_w_h_d[1] == test_w_h_d[1] && train_w_h_d[2] == test_w_h_d[2]) {
 std::cout<< "Size is equal!\n";
 int all_num = train_w_h_d[0]*train_w_h_d[1]*train_w_h_d[2];
 int count = 0;
 for(int i = 0; i < all_num; i++) {
 if(data_train[i] == data_test[i])
 count++;
 }
 if(all_num == count)
 return 1;
 }
 return 0;
}
int main(){
 string root_path = "F:\\SZU\\Projects\\Fetal_Face\\data\\";
 string filepath_train = root_path + "train\\";
 string filepath_test = root_path + "test\\detect_data\\";
 unsigned char* data_train = NULL;
 unsigned char* data_test = NULL;
 int num_train = 127;
 int num_test = 80;
 int train_w_h_d[3] = {0, 0, 0};
 int test_w_h_d[3] = {0, 0, 0};
 for (int i = 0; i < num_train; i++) 
 for (int j = 1; j <= num_test; j++) {
 char num_i[10], num_j[10];
 _itoa_s(i, num_i, 10);
 _itoa_s(j, num_j, 10);
 std::string file_train = filepath_train + "data_" + num_i + ".raw";
 std::string file_test = filepath_test + "data_" + num_j + ".raw";
 if( LoadSample(file_train, data_train, train_w_h_d) )
 std::cout<< "Error: load train data.\n";
 if ( LoadSample(file_test, data_test, test_w_h_d) ) 
 std::cout<< "Error: load test data.\n";
 if( CompareData(data_train, train_w_h_d, data_test, test_w_h_d) ) {
 std::cout<< "Same data: " << i << "-- train\t\t" << j << " -- test\n"<<std::endl;
 system("PAUSE");
 }
 else{
 std::cout<< i << "-- train\t\t" << j << " -- test is difference." <<std::endl;
 }
 delete[] data_train;
 data_train = NULL;
 delete[] data_test;
 data_test = NULL;
 }
 return 0;
}

The code can run correctly but I want to improve the efficiency by the STL. My compiler is VS2008.

So, how to revise it? By the way, what and where is potential error or something be able to develop in my code.

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Sep 4, 2016 at 13:52
\$\endgroup\$
1
  • \$\begingroup\$ The comments on StackOverflow mentioned profiling. If you are worried about performance, that is the best way to go at it. You might want to look at the answers here stackoverflow.com/questions/292457/…. \$\endgroup\$ Commented Sep 4, 2016 at 16:12

1 Answer 1

2
\$\begingroup\$

For inequality, it is sufficient to have one difference. If one difference is found, it's unnecessary to compare more data. There's no point in counting equal data entries and compare that number to the total number of entries, because a single inequality will reduce that number making it impossible to be the same as the total number of entries.

if(data_train[i] == data_test[i])
 count++;

Should be

if(data_train[i] != data_test[i])
 return 0;
answered Sep 4, 2016 at 14:33
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.