Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this C code faster than this C++ code ? getting biggest line in file

I have two versions of a program that does basically the same thing, getting the biggest length of a line in a file, I have a file with about 8 thousand lines, my code in C is a little bit more primitive (of course!) than the code I have in C++. The C programm takes about 2 seconds to run, while the program in C++ takes 10 seconds to run (same file I am testing with for both cases). But why? I was expecting it to take the same amount of time or a little bit more but not 8 seconds slower!

my code in C:

#include <stdio.h> #include <stdlib.h>  #include <string.h>  #if _DEBUG     #define DEBUG_PATH "../Debug/" #else     #define DEBUG_PATH "" #endif  const char FILE_NAME[] = DEBUG_PATH "data.noun";  int main() {        int sPos = 0;     int maxCount = 0;     int cPos = 0;     int ch;     FILE *in_file;                    in_file = fopen(FILE_NAME, "r");     if (in_file == NULL)      {         printf("Cannot open %s\n", FILE_NAME);         exit(8);     }             while (1)      {         ch = fgetc(in_file);         if(ch == 0x0A || ch == EOF) // \n or \r or \r\n or end of file         {                        if ((cPos - sPos) > maxCount)                 maxCount = (cPos - sPos);              if(ch == EOF)                 break;              sPos = cPos;         }         else             cPos++;     }      fclose(in_file);      printf("Max line length: %i\n",  maxCount);       getch();     return (0); } 

my code in C++:

#include <iostream> #include <fstream> #include <stdio.h> #include <string>  using namespace std;  #ifdef _DEBUG     #define FILE_PATH "../Debug/data.noun" #else     #define FILE_PATH "data.noun" #endif  int main() {     string fileName = FILE_PATH;     string s = "";     ifstream file;     int size = 0;      file.open(fileName.c_str());     if(!file)     {         printf("could not open file!");         return 0;     }      while(getline(file, s) )             size = (s.length() > size) ? s.length() : size;     file.close();      printf("biggest line in file: %i", size);         getchar();     return 0; } 
like image 308
Alex Avatar asked Jan 13 '12 15:01

Alex


People also ask

Why is C faster than CPP?

Performance-based on Nature Of Language C++ language is an object-oriented programming language, and it supports some important features like Polymorphism, Abstract Data Types, Encapsulation, etc. Since it supports object-orientation, speed is faster compared to the C language.

Can C++ be as fast as C?

The same code in C and C++ should usually run at exactly the same speed, the exception being code that has different semantics due to different aliasing rules, etc. The difference is between C idioms and C++ idioms.


1 Answers

My guess is that it is a problem with the compiler options you are using, the compiler itself, or the file system. I just now compiled both versions (with optimizations on) and ran them against a 92,000 line text file:

c++ version:  113 ms c version:    179 ms 

And I suspect that the reason that the C++ version is faster is because fgetc is most likely slower. fgetc does use buffered I/O, but it is making a function call to retrieve every character. I've tested it before and fgetc is not as fast as making a call to read the entire line in one call (e.g., compared to fgets).

like image 104
Mark Wilkins Avatar answered Oct 10 '22 03:10

Mark Wilkins