-1

I need to find if the cpu speed is unique using python. Need the equivalent of the below script in python:

cat /proc/cpuinfo | grep MHz | awk -F":" '{print $2}' | uniq | wc -l

This is finding the speed of cpu and check if they are all unique. Returns 1 if unique, more than 1 if not unique.

Any other suggestions to check the same are also invited.

Emlyn Jose
  • 19
  • 4
  • what do you mean by *unique*? Did you see [this](https://stackoverflow.com/questions/4842448/getting-processor-information-in-python)? – Ma0 Nov 28 '17 at 10:55

1 Answers1

1

Just read /proc/cpuinfo and process the input with Python:

with open('/proc/cpuinfo') as f:
    speeds = [line.strip().split(': ')[1] for line in f if line.startswith('cpu MHz')]
    print(len(set(speeds)))

This will print the number of unique cpu speeds: 1 in the unlikely event that all speeds are the same, otherwise > 1.

As a function that returns a bool:

def same_speed():
    with open('/proc/cpuinfo') as f:
        return len(set(line.strip().split(': ')[1] for line in f if line.startswith('cpu MHz'))) == 1
mhawke
  • 84,695
  • 9
  • 117
  • 138