Counting pulses with a frequency of 40kHz should not be a problem.
Printing it every 40 pulses (every 1ms) to the serial monitor at a low baud rate could be.
It surely would be ~250 times too fast for human brains to follow.
Not a good coder here, but I think you should use interrupts for counting pulses.
Are these short pulse trains, or is this a constant frequency.
Maybe you should Google something like "Arduino frequency counter".
Leo..