If I have a program (e.g. a small Python program) on a specific machine. Would the program be able to measure how much energy its computation costed?
I think, there would be two possible ways to go:
A) There is a function that measures the energy of the device before and after the execution of the program.
However, there would be problems concerning the accuracy of the measurement of the device on which the program is executed. Also, at the same time other processes may be executed on the machine, which would (potentially) corrupt the process of measuring. Except for this, it does not seem elegant to me.
B) There is a program that counts the bit-wise computation steps and returns it at the end of the execution. From this, I could possibly calculate the energy consumption of the whole programme knowing the average energy the device takes to carry out one single computation step. However, this seems a bit contradictory to me, since the programme would essentially (at least partly) need to observe itself. Also, I am not sure if this would be a valid measure at all, since it is indirect, and somehow theoretical. -- On the other hand, there would be no perturbations by other programs running on the computer.
Is one of these ways reasonable or are there possibly more convenient of doing this?
Note: I am interested in the real energy usage, not the theoretical. (This is why I think the O-measure of some algorithms within the code would essentially not help me a lot.)
Also: I am not interested in the time measurement, as running the program on different machines will result in different run times. Additionally, knowing the time, does not (in practice) automatically give me the energy usage an all machines.
Rather I am interested in the pure energy usage.