for循环中的++i和i++有什么区别

i++和++i是C系语言的经典课题, 我们知道i++和++i的表面区别为++i的返回值为i+1,而i++则为i,而它们的底层实现分别为: ++i实现: int operator ++ () { return i+1; } i++实现: int operator ++ (int flag) { int j = i; i += 1; return j; } 两者实现的本质区别为其中的j,这个j在程序中称为匿名变量. 现在很明显了,因为这个匿名变量的存在直接导致了i++比++i实际开销大. 那么,是否意味着for(int i = 0;i < n;i++)比for(int i = 0;i < n;++i)开销大呢? 带着这个问题,我想可能需要对比汇编才能看出点区别了. 现在分别创建了两份文件,内容分别为: #include <stdio.h> int main() { for (int i = 0; i < 100; ++i) { printf("%d\n", i); } return 0; } #include <stdio.h> int main() { for (int i = 0; i < 100; i++) { printf("%d\n", i); } return 0; } 然而很遗憾,在使用gcc输出汇编代码后这两份C语言代码输出了完全相同的内容: »

Author image 月杪 on #Code,

快速统计项目代码行数

$ find . -name "*.swift" -or -name "*.h" | xargs wc -l 59 ./mas-cli/AppStore/Downloader.swift 62 ./mas-cli/AppStore/ISStoreAccount.swift 114 ./mas-cli/AppStore/PurchaseDownloadObserver.swift 29 ./mas-cli/AppStore/SSPurchase.swift 24 ./mas-cli/Commands/Account.swift 58 ./mas-cli/Commands/Install.swift 25 ./mas-cli/Commands/List.swift 27 ./mas-cli/Commands/Outdated.swift 85 ./mas-cli/Commands/Reset.swift 63 ./mas-cli/Commands/Search.swift 59 ./mas-cli/Commands/SignIn.swift 18 ./mas-cli/Commands/SignOut.swift 65 ./mas-cli/Commands/Upgrade.swift 21 ./mas-cli/Commands/Version.swift 64 ./mas-cli/Error.swift 34 ./mas-cli/main.swift 44 ./mas-cli/mas-cli-Bridging-Header.h 63 ./mas-cli/NSURLSession+Synchronous.swift 37 ./mas-cli/PrivateHeaders/CommerceKit/CKAccountStore.h 41 ./mas-cli/PrivateHeaders/CommerceKit/CKDownloadQueue.h 38 ./mas-cli/PrivateHeaders/CommerceKit/CKPurchaseController.h 14 ./mas-cli/PrivateHeaders/CommerceKit/CKServiceInterface.h 37 ./mas-cli/PrivateHeaders/CommerceKit/CKSoftwareMap.h 59 ./mas-cli/PrivateHeaders/CommerceKit/CKUpdateController.h 17 ./mas-cli/PrivateHeaders/CommerceKit/ISStoreURLOperationDelegate-Protocol.h 73 ./mas-cli/PrivateHeaders/StoreFoundation/CKSoftwareProduct.h 42 ./mas-cli/PrivateHeaders/StoreFoundation/CKUpdate.h 53 ./mas-cli/PrivateHeaders/StoreFoundation/ISAccountService-Protocol.h 53 ./mas-cli/PrivateHeaders/StoreFoundation/ISAuthenticationContext.h 16 . »

Author image 月杪 on #Code,