Compile: c++ speedread.cpp
Usage: ./a.out [delay_in_ms] < text
Should work on Mac and Linux, and maybe Windows if you uncomment the first line. (Looks like a #include <stdlib.h> needs to be included to make it compile on Linux. Not sure what I don't need that on Mac).
It's kind of fun. I played around with showing words at a constant rate, and with throwing in an extra delay whenever there was punctuation. The version below does the latter. I had thought of this as a possibly good way for displaying text on the limited phones of the early 2000s. Also, I thought it might be an interesting way to display status information on many processes at once. A display line wide enough to display, say, 10 words could be used to display 10 status messages at once instead of one.
Oh...2 clause BSD license, if anyone cares.
//#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
typedef unsigned long DWORD;
bool cont = true;
void
stop( int sig )
{
cont = false;
}
void
Sleep( int msec )
{
usleep( msec * 1000 );
}
DWORD
GetTickCount()
{
static unsigned long base = 0;
struct timeval tv;
gettimeofday( &tv, 0 );
if ( base == 0 )
base = tv.tv_sec;
return (tv.tv_sec - base) * 1000 + tv.tv_usec / 1000;
}
int main( int argc, char *argv[] )
{
int delay = 250;
std::string word;
if ( argc == 2 )
delay = atoi(argv[1]);
signal( SIGINT, stop );
std::cout << "\n\n\n";
int words = 0;
DWORD start = GetTickCount();
std::string display_word = "";
while ( std::cin >> word && cont )
{
Sleep(delay);
++words;
if ( display_word.length() )
display_word += ' ';
display_word += word;
//if ( display_word.length() < 5 )
//continue;
word = display_word;
display_word = "";
int punc = word.find(".");
if ( punc == std::string::npos )
punc = word.find(",");
int pad = 15 - word.length()/2;
for ( int i = 0; i < pad; ++i )
std::cout << " ";
std::cout << " " << word << " \r";
std::cout.flush();
if ( punc != std::string::npos )
Sleep(2*delay);
}
DWORD stop = GetTickCount();
float seconds = (stop - start) / 1000.0;
printf( "\n%d words in %f seconds\n", words, seconds );
if ( seconds != 0 )
printf( "%f words/second, %f words/minute\n", words/seconds,
(60*words)/seconds );
return 0;
}