dty47696 2015-11-10 08:55
浏览 50
已采纳

Simpe循环平方整数值在C中比在PHP中慢

Yesterday I watched a nice (short) video about a mathematics problem with the number '33'. Link: https://www.youtube.com/watch?v=wymmCdLdPvM

In short: Many numbers can be calculated by: a³ + b³ + c³

For example: 29 = 3³ + 1³ + 1³

(should be mentioned: a, b, c can be negative!)

So, I though I give the problem a short bruteforce try. Nothing sophisticated, just to see how fast I can compute those values.

I built 3 Programs (PHP, Pyhton and C, let's concentrate on PHP and C here), and thats what I've got...

PHP-Program:

#!/usr/bin/php5
<?php

$total = 0;
$aktstamp = time();

for ($a = -100000000000; $a <= 100000000000; $a++) {
    for ($b = -100000000000; $b <= 100000000000; $b++) {
        for ($c = -100000000000; $c <= 100000000000; $c++) {
            $total++;

            if ($a**3+$b**3+$c**3 == 33) {
                echo "FOUND IT! a**3+$b**3+$c**3 a: " . $a . " b: " . $b + " c: " . $c;
            }
            if ((-$a)**3+$b**3+$c**3 == 33) {
                echo "FOUND IT! (-a)**3+$b**3+$c**3 a: " . $a . " b: " . $b + " c: " . $c;
            }                   
            if ($a**3+(-$b)**3+$c**3 == 33) {
                echo "FOUND IT! a**3+(-$b)**3+$c**3 a: " . $a . " b: " . $b + " c: " . $c;
            }                   
            if ($a**3+$b**3+(-$c)**3 == 33) {
                echo "FOUND IT! a**3+$b**3+(-$c)**3 a: " . $a . " b: " . $b + " c: " . $c;
            }                   
            if ((-$a)**3+(-$b)**3+$c**3 == 33) {
                echo "FOUND IT! (-a)**3+(-$b)**3+$c**3 a: " . $a . " b: " . $b + " c: " . $c;
            }                   
            if ((-$a)**3+$b**3+(-$c)**3 == 33) {
                echo "FOUND IT! (-a)**3+$b**3+(-$c)**3 a: " . $a . " b: " . $b + " c: " . $c;
            }                   
            if ($a**3+(-$b)**3+(-$c)**3 == 33) {
                echo "FOUND IT! a**3+(-$b)**3+(-$c)**3 a: " . $a . " b: " . $b + " c: " . $c;  
            }

            if ($total % 10000000 == 0) {
                $timetaken = time() - $aktstamp;
                $calcspersec = 10000000 / $timetaken;

                $date = date("d.m.Y", time());
                $time = date("H:i:s", time());
                echo $date . " " . $time . ": " . $a . "
";
                echo "(Calcs per sec: " . $calcspersec . ")
";

                $aktstamp = time();         
            }
        }
    }
}
?>

And a C Program:

/* 
Compile with: 
gcc ./33.c -o 33 -lm -O1
*/

#include <stdio.h>
#include <math.h>
#include <sys/time.h>

long long main(long long argc, char *argv[])
{
    long long total = 0, a = 0, b = 0, c = 0;
    int timetaken, calcspersec;
    int aktstamp = (int)time(NULL);

    for (a = -100000000000; a <= 100000000000; a++) {
        for (b = -100000000000; b <= 100000000000; b++) {
            for (c = -100000000000; c <= 100000000000; c++) {
                total++;

                if (pow(a, 3)+pow(b, 3)+pow(c, 3) == 33) {
                    printf("FOUND IT! pow(a, 3)+pow(b, 3)+pow(c, 3) a: %lld b: %lld c: %lld
", a, b, c);
                }
                if (pow(-a, 3)+pow(b, 3)+pow(c, 3) == 33) {
                    printf("FOUND IT! pow(-a, 3)+pow(b, 3)+pow(c, 3) a: %lld b: %lld c: %lld
", a, b, c);
                }                   
                if (pow(a, 3)+pow(-b, 3)+pow(c, 3) == 33) {
                    printf("FOUND IT! pow(a, 3)+pow(-b, 3)+pow(c, 3) a: %lld b: %lld c: %lld
", a, b, c);
                }                   
                if (pow(a, 3)+pow(b, 3)+pow(-c, 3) == 33) {
                    printf("FOUND IT! pow(a, 3)+pow(b, 3)+pow(-c, 3) a: %lld b: %lld c: %lld
", a, b, c);
                }                   
                if (pow(-a, 3)+pow(-b, 3)+pow(c, 3) == 33) {
                    printf("FOUND IT! pow(-a, 3)+pow(-b, 3)+pow(c, 3) a: %lld b: %lld c: %lld
", a, b, c);
                }                   
                if (pow(-a, 3)+pow(b, 3)+pow(-c, 3) == 33) {
                    printf("FOUND IT! pow(-a, 3)+pow(b, 3)+pow(-c, 3) a: %lld b: %lld c: %lld
", a, b, c);
                }                   
                if (pow(a, 3)+pow(-b, 3)+pow(-c, 3) == 33) {
                    printf("FOUND IT! pow(a, 3)+pow(-b, 3)+pow(-c, 3) a: %lld b: %lld c: %lld
", a, b, c);
                }

                if (total % 10000000 == 0) {
                    timetaken = (int)time(NULL) - aktstamp;
                    calcspersec = 10000000 / timetaken;

                    printf("%lld
", a);
                    printf("(Calcs per sec: %u)
", calcspersec);

                    aktstamp = time(NULL);          
                }
            }
        }
    }
}

Here are the Speeds:

PHP: (Calcs per sec: 2000000)
Python: (Calcs per sec: 1186440)
C: (Calcs per sec: 833333)

It was no suprise to me that PHP was faster than Python. But why on earth is C so much slower than PHP?

Even Python is faster than C...

Do I have a stupid error, or is the C-pow() Function so much slower than PHPs ** ?

So, does anyone see a obvious reason for why C is half as fast as PHP?

  • 写回答

2条回答 默认 最新

  • dpjo15650 2015-11-10 09:04
    关注

    Probably because with the pow() function you are converting your long long's to double, then calling a function, then converting the resulting double back to long long, which are all unnecessary if you would have written a*a*a.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计