aboutsummaryrefslogtreecommitdiff
path: root/widgets
diff options
context:
space:
mode:
authorAdrian C. (anrxc) <anrxc@sysphere.org>2011-03-15 05:02:55 +0100
committerAdrian C. (anrxc) <anrxc@sysphere.org>2011-03-15 05:02:55 +0100
commit753ce61de08350579e2b8016c69b1b058d676fc8 (patch)
tree06c9982723d1a64edf007af4e0be60034f9130b1 /widgets
parentf85d9444d92d1a0be1478a7673dda41b1ab50179 (diff)
downloadvicious-legacy-753ce61de08350579e2b8016c69b1b058d676fc8.tar.xz
cpu: calculation and optimization fixes by Joerg
Diffstat (limited to 'widgets')
-rw-r--r--widgets/cpu.lua28
1 files changed, 12 insertions, 16 deletions
diff --git a/widgets/cpu.lua b/widgets/cpu.lua
index 2673021..319da3e 100644
--- a/widgets/cpu.lua
+++ b/widgets/cpu.lua
@@ -42,33 +42,29 @@ local function worker(format)
end
-- Ensure tables are initialized correctly
- while #cpu_total < #cpu_lines do
- table.insert(cpu_total, 0)
- table.insert(cpu_active, 0)
- table.insert(cpu_usage, 0)
+ for i = #cpu_total + 1, #cpu_lines do
+ cpu_total[i] = 0
+ cpu_usage[i] = 0
+ cpu_active[i] = 0
end
- local total_new = {}
- local active_new = {}
- local diff_total = {}
- local diff_active = {}
for i, v in ipairs(cpu_lines) do
-- Calculate totals
- total_new[i] = 0
+ local total_new = 0
for j = 1, #v do
- total_new[i] = total_new[i] + v[j]
+ total_new = total_new + v[j]
end
- active_new[i] = v[1] + v[2] + v[3]
+ local active_new = total_new - (v[4] + v[5])
-- Calculate percentage
- diff_total[i] = total_new[i] - cpu_total[i]
- diff_active[i] = active_new[i] - cpu_active[i]
- cpu_usage[i] = math.floor(diff_active[i] / diff_total[i] * 100)
+ local diff_total = total_new - cpu_total[i]
+ local diff_active = active_new - cpu_active[i]
+ cpu_usage[i] = math.floor((diff_active / diff_total) * 100)
-- Store totals
- cpu_total[i] = total_new[i]
- cpu_active[i] = active_new[i]
+ cpu_total[i] = total_new
+ cpu_active[i] = active_new
end
return cpu_usage