1

I'm trying to figure out which is faster, as well as learn how to benchmark. Do you think this is an accurate way to test QFile VS ifstream?

http://ideone.com/ipkGh

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
JP_
  • 1,651
  • 14
  • 24
  • 2
    Faster in what way? `QFile` is shorter to type than `ifstream`, so its faster to write ;) – Blender Jun 10 '12 at 02:40
  • 2
    Using `.eof()` as a loop condition is a [bad practice](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Blastfurnace Jun 10 '12 at 02:48
  • what should i use instead of .eof(). – JP_ Jun 10 '12 at 02:55
  • @JeffSchweigler Read the answers in the question linked. It's all there: the whys and the solutions. – R. Martinho Fernandes Jun 10 '12 at 02:56
  • Real answer: _it's hardly relevant in practice_. The real bottleneck is the OS. – Etienne de Martel Jun 10 '12 at 02:56
  • Blender: time it takes to read a file, not type the code :P – JP_ Jun 10 '12 at 02:56
  • 2
    There's generally no universal answer to this question. You would need to implement both in the way you are going to use them and test against your data to determine which is faster for your specific code. An alternative is to use whichever one you like better and profile the whole program once complete. Chances are the things you were worried about being slow aren't even in the top 10 things your program is spending time doing. – Retired Ninja Jun 10 '12 at 02:56
  • excellent thanks @Blastfurnace – JP_ Jun 10 '12 at 03:04

2 Answers2

4

Speed and optimization are 2 important factors for every program. They are more programmer dependent than language or implementation dependent. Many times when we need optimizations we think that current language or implementation is not optimized. ifstream is a standard class in C++ and I think QFile uses that. The speed and the ratio you want to measure can be described and measured in a defined environment and it is better you describe the case you are facing.

Another important point, I can't understand why speed of a file operator class is important to you ?! Many file related operations can and should be done in the memory and traps to hard disks should stay minimum. So in an ideal situatuion I think there is no difference between ifstream , QFile or any slower framework for file access. For you and in general, both of these classes use native OS libraries. I think QFile uses ifstream. ifstream is a wrapper on native OS libraries and QFile is a thicker wrapper because of additional attributes. But I don't think this is very important when you want to use memory in an optimized and managed way.

Kamran Amini
  • 1,012
  • 8
  • 14
0

Well, i'd say they're both fast. You could always check the time that it takes to do the operations for both ifstream and QFile. But like someone said, if you use QFile you write less lines.

Blastcore
  • 340
  • 6
  • 17