Ask Your Question

Make CPU more stable for program timing

asked 2017-01-04 13:36:21 -0600

alfC gravatar image

updated 2017-01-04 15:06:38 -0600

I have a Dell 5720 laptop with i7 3612QM. I am trying to time code and the spread of the timings is comming out quite wide. The code takes about 10 second to run.

I suspect the CPU variable speed feature is producing these random results.

What is the best way to stabilize (not necessarily maximize) the CPU clock speed? Is there a command to do that.

The closest I found is a GUI program called CPUFreqUtility, but is it the correct way? Is there a command line option.

edit retag flag offensive close merge delete


What operating system are you using?

wallyk gravatar imagewallyk ( 2017-01-04 14:37:05 -0600 )edit

@wallyk , Fedora 24 (64bit)

alfC gravatar imagealfC ( 2017-01-04 15:07:40 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2017-01-04 17:08:31 -0600

wallyk gravatar image

updated 2017-01-10 10:51:19 -0600

To time program operations on a modern computer system is—at best—challenging. At worst, there could be no satisfactory solution.

Fedora typically has about 180 living processes during regular system operation, most of which are waiting for an event to occur. In addition, there are dozens of devices which need servicing: system clock, video controller, ethernet, wi-fi, sound, memory paging, disks, USB devices (keyboard, mouse, NVRAM media), etc.

If an ethernet packet comes in while your program is running, the processing for it almost certainly usurps your program's execution temporarily. Maybe the packet causes a network process to run a little which introduces CPU contention. The running process could cause a page fault (to retrieve some code or data which is mapped but not present in RAM) which requires disk i/o to retrieve.

All of these activities contribute to the overall system doing our bidding with rapid choreography to pleasing effect. However, it also means that each program loses the CPU for a few microseconds here and a few milliseconds there.

If your program needs no i/o and the system does not need to respond to anything else, you could run the benchmark program at high priority.

$ sudo nice --adjustment=-20 myprogram

This won't prevent devices from ignoring interrupts, but it will limit the processes which would otherwise compete with the program for CPU. However, this could affect the system in bad ways: break network connections, make the keyboard, mouse, and display unresponsive for a long time, etc.

A more system-friendly technique is to run the benchmark multiple times—maybe hundreds or millions of times—and track the shortest time. If each benchmark run is identical, the lowest time should correspond to the instance with least CPU contention, fewest device interruptions, etc.

If that result isn't acceptable, maybe run the benchmark with the system at runlevel 1? It has the fewest system processes running and with no networking. See man 8 runlevel for more information.

At the extreme, the benchmark could be written to be run directly from the bootloader (grub) without any operating system at all. The downsides are that i/o is difficult to perform and the program has to be written specially to handle all of its system needs. The upside is that you have complete control of the CPU scheduling.

edit flag offensive delete link more



Very detailed response. I guess that this, complemented with some way of disabling the dynamic CPU scaling will be enough.

alfC gravatar imagealfC ( 2017-01-04 18:54:08 -0600 )edit

Question Tools

1 follower


Asked: 2017-01-04 13:36:21 -0600

Seen: 134 times

Last updated: Jan 10 '17