diff options
author | David Phillips <dbphillipsnz@gmail.com> | 2016-05-13 23:10:11 +1200 |
---|---|---|
committer | David Phillips <dbphillipsnz@gmail.com> | 2016-05-13 23:10:11 +1200 |
commit | 8baa386e72bea8d225c95e80e66dac8120db858d (patch) | |
tree | bcc37c5527a9b68b263ea20505235c32d436d75e | |
parent | 4abbf29822a550617afe141705fc83cd33d21410 (diff) | |
download | fractal-gen-8baa386e72bea8d225c95e80e66dac8120db858d.tar.xz |
Refactor 'nice units' RAM calculation
-rw-r--r-- | fractal-gen.c | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/fractal-gen.c b/fractal-gen.c index 38e2a08..1450e91 100644 --- a/fractal-gen.c +++ b/fractal-gen.c @@ -43,6 +43,10 @@ static struct section_generator generators[] = { { "burning-ship-lattice-gen" , &generate_burning_ship_lattice_section } }; +char *ram_units[] = { + "B", "KiB", "MiB", "GiB", "TiB" +}; + int main(int argc, char **argv) { @@ -88,14 +92,17 @@ int main(int argc, char **argv) } ram_nice = (size*size)/clust_total; - if (ram_nice < 1024) - ram_unit = "B"; - else if (ram_nice < 1024*1024) - ram_nice /= 1024, ram_unit = "KiB"; - else if (ram_nice < 1024*1024*1024) - ram_nice /= (1024*1024), ram_unit = "MiB"; - else - ram_nice /= (1024*1024*1024), ram_unit = "GiB"; + + fprintf(stderr, "%f bytes\n", ram_nice); + + i = 1; + ram_unit = ram_units[0]; + while ( ram_nice > 1024 + && i < sizeof(ram_units) / sizeof(ram_units[0])) + { + ram_unit = ram_units[i++]; + ram_nice /= 1024; + } fprintf(stderr, "Forecast resource use:\n" |