2

What is the difference between scanf, gets and fgets (and others?) in terms of efficiency? Example case: imagine reading in a list of several thousand integers (per line)

This question has been asked more often, but everyone always responds by mentioning buffer overflows (eg: here), so to clarify why my question is not in fact a duplicate: I know and do not care about buffer overflows. The setting in which I would apply these methods are in competitive programming, where I know exactly what the input looks like and even when the input is not as expected (ie. leading to buffer overflows) it's not my problem.

Community
  • 1
  • 1
Rick
  • 308
  • 3
  • 8
  • 1
    `I would apply these methods are in competitive programming, where I know exactly what the input looks like and even when the input is not as expected`... heh? I'm not participating, for sure. – Sourav Ghosh Nov 10 '16 at 12:06
  • 2
    Shouldn't be so hard to make a small program which does just what you want, and benchmark it. Though, that only benchmark it on *your* system. Other compiler and libraries might have other implementations which might not work exactly the same as the functions in your system. – Some programmer dude Nov 10 '16 at 12:08
  • All of those are char by char. Use `read` for speed. – stark Nov 10 '16 at 12:17
  • `scanf` needs to interpret the format string, where as fgets just reads one line. So if you just want to read a file containing one number per line it's probably faster using `fgets` and parse the line with `strtol` than using `scanf`. To be sure just find out by yourself by makeing a benchmark as already suggested. – Jabberwocky Nov 10 '16 at 12:20
  • If speed efficiency is paramount, consider `fread()`, likely to be faster or at least a contender. – chux - Reinstate Monica Nov 10 '16 at 14:00
  • Thay say that until scanf can not report overflow in variables : It is not 100% sure for gets the input number. Would better use fgets, strtoul or strtol I not remember well – RosLuP Nov 10 '16 at 14:30
  • `scanf, fgets` read text, they do not read integers. `scanf()` can convert user input to various types including `int`. `gets()` is no longer is a valid standard C library function. But performance depends on many things including what you want to do with the data, degree of portability, prior knowledge of the maximum size of the input including integer range. Efficiency is measure in speed, memory usage code usage, terseness of coding. This post needs more detail for quality answers. – chux - Reinstate Monica Nov 10 '16 at 15:27
  • See [Why `gets()` is too dangerous to be used — ever!](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) for an explanation of why `gets()` should never, ever be used. Note that `read()` and `fread()` do not stop reading at a newline in general; they will read past newlines, or not read up to newlines, depending on how you use them. They're probably not appropriate for reading lines of data. Note that `scanf()` is casual about newlines too; it will cheerfully ignore them most of the time. – Jonathan Leffler Nov 11 '16 at 03:23
  • The only way to authoritatively answer this question is to code up multiple versions and benchmark them, bearing in mind that performance will be a function of everything between the compiler, optimization settings, the platform's I/O system, etc. – John Bode Nov 11 '16 at 16:07
  • I was hoping someone had already tested this, and had an answer readily available, had a bit of time pressure at the time. I will do some comparison myself this coming weekend, if I have the time with various different parameters. – Rick Nov 23 '16 at 19:14

0 Answers0