Frequency Counting on a Raspberry PI
The Pi is not really the best tool for the job.  You have an entire operating system vying for processing time, with many processes running at the same time as yours. The only way you would be able to do it reliably would be to directly interface with the hardware and use timers and such to do the job.

Certainly trying to do anything where reading rapidly from a GPIO is going to only work at low speeds, simply because your program isn't running all the time - it's sharing time with everything else the Pi is doing.

You would be better off using a dedicated microcontroller as an interface to the Pi which takes the square wave, calculates the frequency, then reports it over a serial protocol, such as the UART or SPI connections.