Printf is a very heavy function. It should only be used for generating natural language strings. It should never be used in performance code due to the format parsing overhead.
For performance code use some form of stream directly.
Use an input library. In Linux this is probably done by opening an input stream from the input driver file. In Windows you use their input library from DirectX. Some Window management libraries have input as part of them.
I dont know about the input libraries you are talking about(DirectX libs etc), but considering C++ streams vs C printf, check this script:
C++:
#include <iostream>
#include <cstdio>
#include <string>
class Benchmark{
long long startT;
bool paused;
long long lastPRet;
public:
Benchmark() : startT(__rdtsc()), paused(false), lastPRet(0) {}
long long pause()
{
if (paused)
return lastPRet;
paused = true;
return lastPRet = __rdtsc() - startT;
}
long long get()
{
return __rdtsc() - startT;
}
void start()
{
paused = false;
startT = __rdtsc();
}
};
#include <random>
#include <chrono>
int main()
{
const int LOOPTIMES = 10000;
const float CPU_CLOCK_APPROX = 3600000000.f;
std::vector<int> randInfo;
auto currentTime = std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::mt19937 generator(currentTime);
//generate random info:
for(int i = 0; i < LOOPTIMES; ++i)
{
randInfo.push_back(generator());
}
Benchmark m;
m.start();
for(int i = 0; i < LOOPTIMES; ++i)
{
printf("Number %d: %d\n", i, randInfo[i]);
}
auto pPrintf = m.get() / CPU_CLOCK_APPROX;
m.start();
for(int i = 0; i < LOOPTIMES; ++i)
{
std::cout << "Number " << i << ": " << randInfo[i] << "\n";
}
auto pCout = m.get() / CPU_CLOCK_APPROX;
std::cout << "\n\n\nLooping " << LOOPTIMES << " times:\n";
std::cout << "\tprinf(): " << pPrintf << " sec\n";
std::cout << "\tcout << ...: " << pCout << " sec\n";
std::cin.get();
}
this script outputs:
Looping 10000 times:
printf(): 0.972621 sec
cout << ...: 14.7 secs
Yes the numbers vary between runs roughly by 10%, but still as you see, printf is a loot quicker than C++ ostream object cout.
This is because while printf has some formatting to do, it is still only 1 function call, and the equivalent cout is actually 5 function calls. Dont forget that ostream inherits from basic_ios and that inherits from ios_base. + the guts of << operator has to check whether the stream state is goodbit(otherwise the operation cannot proceed), it must construct sentry and do checks with that, then it has to get the buf object(streambuf type) and it all calls virtual methods, which must be dispatched, further increasing overhead.
The code was run on default Release mode on Visual studio 2012.
On the other hand, printf has no compile-time checks for types passed, so even printf("%d", std::vector<std::string>(5)[0]); is valid, but will result in garbage printed.
Back on topic, what you want to do is not nativelly possible, you must use System specific ways to do it. Here is a link Ive found:
http://stackoverflow.com/questions/...dard-input-without-waiting-for-enter-to-be-pr that may be of help