euler87: Rust und C durch Schleifentausch beschleunigen
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.
This commit is contained in:
parent
1383a3f62c
commit
84c3f80ab0
2 changed files with 17 additions and 12 deletions
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Reference in a new issue