QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 1987|回复: 0
打印 上一主题 下一主题

为什么 C++ 只比 VBA 快 4倍?

[复制链接]
字体大小: 正常 放大
蕾欧娜        

19

主题

9

听众

25

积分

升级  21.05%

  • TA的每日心情
    郁闷
    2015-4-14 11:21
  • 签到天数: 1 天

    [LV.1]初来乍到

    自我介绍
    德玛西亚
    跳转到指定楼层
    1#
    发表于 2015-4-20 10:09 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta
    用C++和VBA分别写了一段用随机数字测试的值的算法,用500万个随机数,发现VBA需要570毫秒,而C++则需要150毫秒。这也太让人失望了把,C++写起来那么麻烦,起码也要快个几个数量级吧?python就更别说了,居然要7秒钟。求各位大神帮我看看是不是我的代码没优化好?" I2 e$ n$ I- q) x3 a
    (平台是Visual Studio,已经开了Release模式)
    - X/ s0 }7 k2 Z2 H2 e6 p: _
    8 [: T+ h0 [6 H! G8 j计算方法:生成,两个在0 到 之间的随机数,数一数这些数字里面有多少个落在了半径为的1/4圆的扇形里面,用这个数字代表扇形面积,用总随机数数量代表正方形面积。因为扇形的面积是 ,而正方形的面积是, 可得知=扇形面积/正方形面积 * 45 p# @: @- \* o, z1 Y
    * J+ b' b: N, K$ E/ R
    C++代码:6 h0 g, m; S8 Y! }) d+ Y4 M! ]
    #include "stdafx.h"#include <iostream>#include <time.h>using namespace std;void main(){        double st = clock();        double rand_max = 32767;        srand((int)time(0));        unsigned int simulate_total = 2500000;        unsigned int inside_count = 0;        unsigned int radius = rand_max * rand_max;        unsigned int randA;        unsigned int randB;        unsigned int randA_opp;        unsigned int randB_opp;        for (unsigned int i = 1; i < simulate_total; i++){                randA = rand();                randB = rand();                if ((randA * randA + randB * randB) < radius){                        inside_count++;                }                randA_opp = rand_max - randA;                randB_opp = rand_max - randB;                if ((randA_opp * randA_opp + randB_opp * randB_opp) < radius){                        inside_count++;                }                }        cout << inside_count / double(simulate_total) * 2 << endl;        cout << clock() - st << endl;}  S6 r) U1 e! F# \+ o# z$ P$ A; @

    3 _. ~- T# n0 M* o: f- T4 k( `+ B! ^
      ~6 b3 |1 a' D. T2 S3 F' dVBA代码:) u: ]% y. |( {5 o
    Sub simulate_pi()Dim area_count As Double: area_count = 0Dim simulate_count As Double: simulate_count = 2500000Dim i As DoubleDim randA As Double, randB As DoubleDim randA_opp As Double, randB_opp As DoubleFor i = 1 To simulate_count    randA = Math.Rnd()    randB = Math.Rnd()    randA_opp = 1 - randA    randB_opp = 1 - randB    If randA * randA + randB * randB < 1 Then        area_count = area_count + 1    End If    If randA_opp * randA_opp + randB_opp * randB_opp < 1 Then        area_count = area_count + 1    End IfNext iDebug.Print "Estimate: ", area_count / simulate_count * 2End Sub'VBA内测时间的方法:'新建一个module,把以下代码复制进去,然后运行test.Option ExplicitPrivate Declare Function getFrequency Lib "kernel32" _Alias "QueryPerformanceFrequency" (cyFrequency As Currency) As LongPrivate Declare Function getTickCount Lib "kernel32" _Alias "QueryPerformanceCounter" (cyTickCount As Currency) As Long'Function MicroTimer() As Double'' Returns seconds.'    Dim cyTicks1 As Currency    Static cyFrequency As Currency    '    MicroTimer = 0' Get frequency.    If cyFrequency = 0 Then getFrequency cyFrequency' Get ticks.    getTickCount cyTicks1' Seconds    If cyFrequency Then MicroTimer = cyTicks1 / cyFrequencyEnd FunctionSub test()    Dim st: st = MicroTimer    Call simulate_pi    Debug.Print (MicroTimer - st) * 1000End Sub
    + f5 j  m" f) B- F* l. N. H; M! V% T1 D# [3 s' T2 q
    % X" H" _% F6 i9 P* u6 K) r) n$ A
    Python代码:#!/usr/bin/python# Filename : pi_simulate.pyimport randomimport timetime_start = time.perf_counter()inside_count = 0simulate_total = 2500000randA = 0randB = 0for i in range(1, simulate_total):    randA = random.uniform(0,1)    randB = random.uniform(0,1)    if ((randA * randA + randB * randB) < 1):        inside_count = inside_count + 1    randA_opp = 1 - randA    randB_opp = 1 - randB    if ((randA_opp * randA_opp + randB_opp * randB_opp) < 1):        inside_count = inside_count + 1print (inside_count/simulate_total*2)print ("Time spent", time.perf_counter() - time_start)
    ; d( `: d: y+ i6 F- L2 A9 \# j" c) R2 L+ d" [. e; J( |

    * U! l' E# H! r. w
    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2025-7-29 10:05 , Processed in 0.356870 second(s), 49 queries .

    回顶部