sudo powermetrics will display information that tells you about your core clock frequency. The output is very different between Intel and ARM processors.
It seems that on Intel processors the actual clock frequency remains unchanged, but the CPUs will be unused some part of the time. So you can have output like
CPU Average frequency as fraction of nominal: 85.40% (1964.20 Mhz)
which means your CPU runs at 2,300 MHz, but only 85.40% of the time. And that is reported for each individual CPU. I don't know what happens in a "turbo" mode where the clock speed should supposedly be higher, or if the clock speed goes down if the computer gets too hot.
An M1 or M2 processors has different output. On an M1 Pro, the eight performance cores can each run at 15 different speeds from 600 MHz to 3,328 MHz; efficiency cores have 4 speeds from 600 MHz to 2,064 MHz. The tool reports for each core what percentage of time it spent at which clock speed. And each processor can be idle; as a type this, total idle time is about 796% for the performance cores, so they are only used 4% of the time.
So in this case you have eight cores, each capable of running at 3,328. Your decision is now what you call the clock speed. You could call it 3,328 MHz until you get all cores running and maybe the clock speed goes down a bit due to heat.
Now programmatically: This tool is sudo only, so you can't easily launch it from an app. On an M1 Pro, you can use clock_gettime_nsec_mp (CLOCK_THREAD_CPUTIME_ID) to get the time in the current thread with a resolution of 42 nanoseconds on that particular processor (faster and higher resolution than clock()). Then you write a loop that runs for example 1,000,000 iterations of a loop with 1 cycle per clock, measure the time, and calculate the clock speed from that. clock_gettime_nsec_mp is quite stable, so three measurements and throwing away the worst outlier should be fine. It may be a bit difficult to write a loop that doesn't get completely optimised away :-)
If you want Intel clock speeds as well, you may need a different loop, and check how many cycles it takes. And you must distinguish which processor is used, and avoid using Rosetta because Rosetta runs ARM code but thinks it is running on Intel.