From 84c3f80ab077f71f2b0f8e5fde67cd8b7ae696ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Sommer?= Date: Mon, 8 May 2017 11:03:54 +0200 Subject: [PATCH] euler87: Rust und C durch Schleifentausch beschleunigen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wenn man die innere (hoch 4) und die äußere Schleife (hoch 2) tauscht, wird das C-Programm ca. 10% und das Rust-Programm ca. 25% schneller. --- 2017-03-28/jo-so_euler87.c | 16 +++++++++------- 2017-03-28/jo-so_euler87.rs | 13 ++++++++----- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/2017-03-28/jo-so_euler87.c b/2017-03-28/jo-so_euler87.c index 5c0a6d7..43fd23c 100644 --- a/2017-03-28/jo-so_euler87.c +++ b/2017-03-28/jo-so_euler87.c @@ -104,22 +104,24 @@ int main(void) vector_uint nums; vector_init(&nums); - for (uint* pn2 = prime_numbers; pn2 < prime_numbers_end; ++pn2) + for (uint* pn4 = prime_numbers; pn4 < prime_numbers_end; ++pn4) { - uint pow2 = *pn2 * *pn2; + uint pow4 = *pn4 * *pn4; + pow4 *= pow4; + if (pow4 >= LIMIT) + break; for (uint* pn3 = prime_numbers; pn3 < prime_numbers_end; ++pn3) { - uint sum = pow2 + *pn3 * *pn3 * *pn3; + uint sum = pow4 + *pn3 * *pn3 * *pn3; if (sum >= LIMIT) break; - for (uint* pn4 = prime_numbers; pn4 < prime_numbers_end; ++pn4) + for (uint* pn2 = prime_numbers; pn2 < prime_numbers_end; ++pn2) { - uint pow4 = *pn4 * *pn4; - pow4 *= pow4; + uint pow2 = *pn2 * *pn2; - uint sum2 = sum + pow4; + uint sum2 = sum + pow2; if (sum2 >= LIMIT) break; diff --git a/2017-03-28/jo-so_euler87.rs b/2017-03-28/jo-so_euler87.rs index c5611e1..ba819d8 100644 --- a/2017-03-28/jo-so_euler87.rs +++ b/2017-03-28/jo-so_euler87.rs @@ -35,17 +35,20 @@ fn main() { let mut nums = Vec::new(); - for pn2 in pn.iter() { - let pow2 = pn2.pow(2); + for pn4 in pn.iter() { + let pow4 = pn4.pow(4); + if pow4 >= LIMIT { + break; + } for pn3 in pn.iter() { - let sum = pow2 + pn3.pow(3); + let sum = pow4 + pn3.pow(3); if sum >= LIMIT { break; } - for pn4 in pn.iter() { - let sum2 = sum + pn4.pow(4); + for pn2 in pn.iter() { + let sum2 = sum + pn2.pow(2); if sum2 >= LIMIT { break; }