pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

而 (1) Vs.for (;;) 有速度差異嗎?

while (1) Vs. for (;;) Is there a speed difference?(而 (1) Vs.for (;;) 有速度差異嗎?)
本文介紹了而 (1) Vs.for (;;) 有速度差異嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

長版...

今天一位同事在看到我在 Perl 腳本中使用 while (1) 后斷言 for (;;) 更快.我認為它們應該是相同的,希望解釋器可以優化任何差異.我設置了一個腳本,該腳本將運行 1,000,000,000 次循環迭代和相同數量的 while 循環并記錄之間的時間.我找不到明顯的區別.我的同事說一位教授告訴他 while (1) 正在做一個比較 1 == 1for (;;) 不是.我們用 100 倍的 C++ 迭代次數重復了相同的測試,差異可以忽略不計.然而,這是一個圖形示例,說明編譯代碼與腳本語言相比可以快多少.

A co-worker asserted today after seeing my use of while (1) in a Perl script that for (;;) is faster. I argued that they should be the same hoping that the interpreter would optimize out any differences. I set up a script that would run 1,000,000,000 for loop iterations and the same number of while loops and record the time between. I could find no appreciable difference. My co-worker said that a professor had told him that the while (1) was doing a comparison 1 == 1 and the for (;;) was not. We repeated the same test with the 100x the number of iterations with C++ and the difference was negligible. It was however a graphic example of how much faster compiled code can be vs. a scripting language.

簡短版本...

如果您需要擺脫無限循環,是否有任何理由更喜歡 while (1) 而不是 for (;;)?

Is there any reason to prefer a while (1) over a for (;;) if you need an infinite loop to break out of?

注意:如果問題不清楚.這純粹是幾個朋友之間有趣的學術討論.我知道這不是一個所有程序員都應該為之苦惱的非常重要的概念.感謝所有出色的回答,我(以及我相信其他人)從這次討論中學到了一些東西.

Note: If it's not clear from the question. This was purely a fun academic discussion between a couple of friends. I am aware this is not a super important concept that all programmers should agonize over. Thanks for all the great answers I (and I'm sure others) have learned a few things from this discussion.

更新:上述同事權衡了以下回復.

Update: The aforementioned co-worker weighed in with a response below.

在這里引用以防它被掩埋.

Quoted here in case it gets buried.

它來自一個 AMD 匯編程序員.他說 C 程序員(人們)沒有意識到他們的代碼效率低下.他說不過今天,gcc 編譯器非常好,把像他這樣的人趕出去的業務.例如,他說,并告訴我 while 1for(;;).我現在出于習慣使用它,但是 gcc 尤其是解釋器這兩天將執行相同的操作(處理器跳轉),因為它們已經過優化.

It came from an AMD assembly programmer. He stated that C programmers (the poeple) don't realize that their code has inefficiencies. He said today though, gcc compilers are very good, and put people like him out of business. He said for example, and told me about the while 1 vs for(;;). I use it now out of habit but gcc and especially interpreters will do the same operation (a processor jump) for both these days, since they are optimized.

推薦答案

在 perl 中,它們產生相同的操作碼:

In perl, they result in the same opcodes:

$ perl -MO=Concise -e 'for(;;) { print "foo
" }'
a  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 2 -e:1) v ->3
9     <2> leaveloop vK/2 ->a
3        <{> enterloop(next->8 last->9 redo->4) v ->4
-        <@> lineseq vK ->9
4           <;> nextstate(main 1 -e:1) v ->5
7           <@> print vK ->8
5              <0> pushmark s ->6
6              <$> const[PV "foo
"] s ->7
8           <0> unstack v ->4
-e syntax OK

$ perl -MO=Concise -e 'while(1) { print "foo
" }'
a  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 2 -e:1) v ->3
9     <2> leaveloop vK/2 ->a
3        <{> enterloop(next->8 last->9 redo->4) v ->4
-        <@> lineseq vK ->9
4           <;> nextstate(main 1 -e:1) v ->5
7           <@> print vK ->8
5              <0> pushmark s ->6
6              <$> const[PV "foo
"] s ->7
8           <0> unstack v ->4
-e syntax OK

同樣在 GCC 中:

#include <stdio.h>

void t_while() {
    while(1)
        printf("foo
");
}

void t_for() {
    for(;;)
        printf("foo
");
}

    .file   "test.c"
    .section    .rodata
.LC0:
    .string "foo"
    .text
.globl t_while
    .type   t_while, @function
t_while:
.LFB2:
    pushq   %rbp
.LCFI0:
    movq    %rsp, %rbp
.LCFI1:
.L2:
    movl    $.LC0, %edi
    call    puts
    jmp .L2
.LFE2:
    .size   t_while, .-t_while
.globl t_for
    .type   t_for, @function
t_for:
.LFB3:
    pushq   %rbp
.LCFI2:
    movq    %rsp, %rbp
.LCFI3:
.L5:
    movl    $.LC0, %edi
    call    puts
    jmp .L5
.LFE3:
    .size   t_for, .-t_for
    .section    .eh_frame,"a",@progbits
.Lframe1:
    .long   .LECIE1-.LSCIE1
.LSCIE1:
    .long   0x0
    .byte   0x1
    .string "zR"
    .uleb128 0x1
    .sleb128 -8
    .byte   0x10
    .uleb128 0x1
    .byte   0x3
    .byte   0xc
    .uleb128 0x7
    .uleb128 0x8
    .byte   0x90
    .uleb128 0x1
    .align 8
.LECIE1:
.LSFDE1:
    .long   .LEFDE1-.LASFDE1
.LASFDE1:
    .long   .LASFDE1-.Lframe1
    .long   .LFB2
    .long   .LFE2-.LFB2
    .uleb128 0x0
    .byte   0x4
    .long   .LCFI0-.LFB2
    .byte   0xe
    .uleb128 0x10
    .byte   0x86
    .uleb128 0x2
    .byte   0x4
    .long   .LCFI1-.LCFI0
    .byte   0xd
    .uleb128 0x6
    .align 8
.LEFDE1:
.LSFDE3:
    .long   .LEFDE3-.LASFDE3
.LASFDE3:
    .long   .LASFDE3-.Lframe1
    .long   .LFB3
    .long   .LFE3-.LFB3
    .uleb128 0x0
    .byte   0x4
    .long   .LCFI2-.LFB3
    .byte   0xe
    .uleb128 0x10
    .byte   0x86
    .uleb128 0x2
    .byte   0x4
    .long   .LCFI3-.LCFI2
    .byte   0xd
    .uleb128 0x6
    .align 8
.LEFDE3:
    .ident  "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
    .section    .note.GNU-stack,"",@progbits

所以我想答案是,它們在許多編譯器中都是一樣的.當然,對于其他一些編譯器來說,情況可能不一定如此,但循環內的代碼很可能比循環本身貴幾千倍,所以誰在乎?

So I guess the answer is, they're the same in many compilers. Of course, for some other compilers this may not necessarily be the case, but chances are the code inside of the loop is going to be a few thousand times more expensive than the loop itself anyway, so who cares?

這篇關于而 (1) Vs.for (;;) 有速度差異嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 东莞喷砂机-喷砂机-喷砂机配件-喷砂器材-喷砂加工-东莞市协帆喷砂机械设备有限公司 | 缝纫客| 掺铥光纤放大器-C/L波段光纤放大器-小信号光纤放大器-合肥脉锐光电技术有限公司 | 优考试_免费在线考试系统_培训考试系统_题库系统_组卷答题系统_匡优考试 | 重庆小面培训_重庆小面技术培训学习班哪家好【终身免费复学】 | 2025第九届世界无人机大会 | 棉柔巾代加工_洗脸巾oem_一次性毛巾_浴巾生产厂家-杭州禾壹卫品科技有限公司 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 钛板_钛管_钛棒_钛盘管-无锡市盛钛科技有限公司| 承插管件_不锈钢承插管件_锻钢高压管件-温州科正阀门管件有限公司 | 模切之家-专注服务模切行业的B2B平台! | 上海地磅秤|电子地上衡|防爆地磅_上海地磅秤厂家–越衡称重 | 流变仪-热分析联用仪-热膨胀仪厂家-耐驰科学仪器商贸 | 成都装修公司-成都装修设计公司推荐-成都朗煜装饰公司 | 家庭教育吧-在线家庭教育平台,专注青少年家庭教育 | 生物除臭剂-除味剂-植物-污水除臭剂厂家-携葵环保有限公司 | 精益专家 - 设备管理软件|HSE管理系统|设备管理系统|EHS安全管理系统 | 无压烧结银_有压烧结银_导电银胶_导电油墨_导电胶-善仁(浙江)新材料 | 厂房出售_厂房仓库出租_写字楼招租_土地出售-中苣招商网-中苣招商网 | 【化妆品备案】进口化妆品备案流程-深圳美尚美化妆品有限公司 | 注塑模具_塑料模具_塑胶模具_范仕达【官网】_东莞模具设计与制造加工厂家 | 组织研磨机-高通量组织研磨仪-实验室多样品组织研磨机-东方天净 传递窗_超净|洁净工作台_高效过滤器-传递窗厂家广州梓净公司 | 除尘器布袋骨架,除尘器滤袋,除尘器骨架,电磁脉冲阀膜片,卸灰阀,螺旋输送机-泊头市天润环保机械设备有限公司 | 首页_中夏易经起名网| 洁净化验室净化工程_成都实验室装修设计施工_四川华锐净化公司 | 昊宇水工|河北昊宇水工机械工程有限公司 | 湖南档案密集架,智能,物证,移动,价格-湖南档案密集架厂家 | 99文库_实习生实用的范文资料文库站 | 不干胶标签,不干胶标签纸_厂家-山东同力胶粘制品 | 昆明挖掘机修理厂_挖掘机翻新再制造-昆明聚力工程机械维修有限公司 | 生产自动包装秤_颗粒包装秤_肥料包装秤等包装机械-郑州鑫晟重工科技有限公司 | 在线钠离子分析仪-硅酸根离子浓度测定仪-油液水分测定仪价格-北京时代新维测控设备有限公司 | 阜阳在线-阜阳综合门户| 温州富欧金属封头-不锈钢封头厂家 | 十字轴_十字轴万向节_十字轴总成-南京万传机械有限公司 | 北京森语科技有限公司-模型制作专家-展览展示-沙盘模型设计制作-多媒体模型软硬件开发-三维地理信息交互沙盘 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 咖啡加盟,咖啡店加盟连锁品牌-卡小逗 | 分子精馏/精馏设备生产厂家-分子蒸馏工艺实验-新诺舜尧(天津)化工设备有限公司 | 冷却塔厂家_冷却塔维修_冷却塔改造_凉水塔配件填料公司- 广东康明节能空调有限公司 | Maneurop/美优乐压缩机,活塞压缩机,型号规格,技术参数,尺寸图片,价格经销商 |