High Performance Compiled Hit Counter
TextCounter C++ is a compiled version of the popular TextCounter script. Written in C++, it provides the same text-based hit counting functionality but executes significantly faster with solid file locking for high-concurrency environments. Perfect for busy websites that need reliable visitor tracking.
The C++ version of TextCounter offers dramatic performance improvements over the Perl version. As a compiled binary with optimized file locking, it handles concurrent requests efficiently and uses minimal server resources.
| Metric | Perl Version | C++ Version |
|---|---|---|
| Execution Time | ~20-50ms | ~1-3ms |
| Memory Usage | 2-5 MB | ~150 KB |
| Requests/Second | ~50 | ~800 |
| File Locking | Basic | Solid |
| Modification Ease | Easy (edit script) | Moderate (recompile) |
| File | Description |
|---|---|
textcounter.cpp |
C++ source code with file locking |
Makefile |
Compilation instructions |
README |
Compilation and installation guide |
Executes 15-25x faster than Perl version. Handles 800+ requests per second.
Advanced locking prevents count corruption under heavy concurrent access.
Uses only 150KB of memory compared to 2-5MB for Perl.
Outputs plain text numbers that you can style with CSS however you want.
Minimal CPU consumption per request. Native compiled code runs efficiently.
Type-safe C++ code reduces runtime errors and improves stability.
textcounter.cpp and Makefile to your server.
textcounter.cpp and set the DATA_FILE constant to your desired path.
g++ -O2 -o textcounter textcounter.cpp or simply make
touch /var/www/data/counter.datchmod 666 /var/www/data/counter.dat
chmod 755 textcounter
http://yoursite.com/cgi-bin/textcounter
<!--#exec cgi="/cgi-bin/textcounter"-->
To start your counter at a specific number: echo "10000" > /var/www/data/counter.dat
#include <iostream>
#include <fstream>
#include <sys/file.h>
#include <unistd.h>
using namespace std;
// Configuration - Edit this path
const char* DATA_FILE = "/var/www/data/counter.dat";
int main() {
// Output HTTP header
cout << "Content-type: text/plain\n\n";
int count = 0;
// Open file for reading and writing
int fd = open(DATA_FILE, O_RDWR | O_CREAT, 0666);
if (fd == -1) {
cerr << "Error: Cannot open data file" << endl;
return 1;
}
// Acquire exclusive lock
if (flock(fd, LOCK_EX) == -1) {
cerr << "Error: Cannot lock file" << endl;
close(fd);
return 1;
}
// Read current count
ifstream infile(DATA_FILE);
if (infile.is_open()) {
infile >> count;
infile.close();
}
// Increment count
count++;
// Write new count
ofstream outfile(DATA_FILE, ios::trunc);
if (outfile.is_open()) {
outfile << count;
outfile.close();
}
// Release lock
flock(fd, LOCK_UN);
close(fd);
// Output count
cout << count;
return 0;
}
CC = g++
CFLAGS = -O2 -Wall
TARGET = textcounter
all: $(TARGET)
$(TARGET): textcounter.cpp
$(CC) $(CFLAGS) -o $(TARGET) textcounter.cpp
clean:
rm -f $(TARGET)
install: $(TARGET)
cp $(TARGET) /usr/local/apache/cgi-bin/
chmod 755 /usr/local/apache/cgi-bin/$(TARGET)
.PHONY: all clean install
<!--#exec cgi="/cgi-bin/textcounter"-->
You are visitor number <span class="counter"><!--#exec cgi="/cgi-bin/textcounter"--></span>
<style>
.counter {
font-family: 'Courier New', monospace;
font-size: 2em;
color: #00ff00;
background: #000;
padding: 5px 15px;
border-radius: 5px;
display: inline-block;
}
</style>
<table border="1" bgcolor="#000000" cellpadding="5">
<tr>
<td>
<font color="#00ff00" face="Courier New" size="+2">
<b>Visitor #<!--#exec cgi="/cgi-bin/textcounter"--></b>
</font>
</td>
</tr>
</table>
C++ source code with file locking
Compilation automation
Compilation and installation guide
echo "0" > /var/www/data/counter.dat. To start at a specific number like 1000: echo "1000" > /var/www/data/counter.dat