Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ : How expensive is ofstream.tellp()?

Tags:

c++

I want to call it in a tight loop thousands of times per second . Is it an expensive call? I am using Windows Visual C++ .

like image 646
GabiMe Avatar asked Dec 23 '22 01:12

GabiMe


2 Answers

This is an old question, but I will answer anyway, in case there will be other people looking for an answer.

So we have a logging library, which uses streams and tellp for determining the size of the file. It did call tellp for every log being made.

So with tellp 1 log line would take aprox 20 microseconds depending on the machine. Without it the log takes about 1 microsecond.

So yes depending on your domain it might be very expensive or it might not matter at all.

p.s. first rule of optimisation - don't optimise..

like image 89
Tadzys Avatar answered Jan 07 '23 23:01

Tadzys


C++ doesn't mandate the performance (in seconds) of any particular parts of that standard library (although many containers and algorithms have complexity requirements).

This means that you are at the mercy of your implementation. The only reliable thing to do is to measure it and see if it is acceptable in your application.

like image 26
CB Bailey Avatar answered Jan 08 '23 01:01

CB Bailey