久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 PHP 中將字符串轉換為整數的最快方法

Fastest way to convert string to integer in PHP(在 PHP 中將字符串轉換為整數的最快方法)
本文介紹了在 PHP 中將字符串轉換為整數的最快方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

使用 PHP,將這樣的字符串轉換為整數的最快方法是什么:"123"?

Using PHP, what's the fastest way to convert a string like this: "123" to an integer?

為什么那個特定的方法是最快的?如果它收到意外的輸入,例如 "hello" 或數組,會發生什么?

Why is that particular method the fastest? What happens if it gets unexpected input, such as "hello" or an array?

推薦答案

我剛剛設置了一個快速的基準測試練習:

I've just set up a quick benchmarking exercise:

Function             time to run 1 million iterations
--------------------------------------------
(int) "123":                0.55029
intval("123"):              1.0115  (183%)

(int) "0":                  0.42461
intval("0"):                0.95683 (225%)

(int) int:                  0.1502
intval(int):                0.65716 (438%)

(int) array("a", "b"):      0.91264
intval(array("a", "b")):    1.47681 (162%)

(int) "hello":              0.42208
intval("hello"):            0.93678 (222%)

平均而言,調用 intval() 慢兩倍半,如果您的輸入已經是整數,則差異最大.

On average, calling intval() is two and a half times slower, and the difference is the greatest if your input already is an integer.

我很想知道為什么.

更新:我再次運行測試,這次使用強制(0 + $var)

Update: I've run the tests again, this time with coercion (0 + $var)

| INPUT ($x)      |  (int) $x  |intval($x) |  0 + $x   |
|-----------------|------------|-----------|-----------|
| "123"           |   0.51541  |  0.96924  |  0.33828  |
| "0"             |   0.42723  |  0.97418  |  0.31353  |
| 123             |   0.15011  |  0.61690  |  0.15452  |
| array("a", "b") |   0.8893   |  1.45109  |  err!     |
| "hello"         |   0.42618  |  0.88803  |  0.1691   |
|-----------------|------------|-----------|-----------|

附錄:我剛剛遇到了一個稍微出乎意料的行為,您在選擇其中一種方法時應該注意:

Addendum: I've just come across a slightly unexpected behaviour which you should be aware of when choosing one of these methods:

$x = "11";
(int) $x;      // int(11)
intval($x);    // int(11)
$x + 0;        // int(11)

$x = "0x11";
(int) $x;      // int(0)
intval($x);    // int(0)
$x + 0;        // int(17) !

$x = "011";
(int) $x;      // int(11)
intval($x);    // int(11)
$x + 0;        // int(11) (not 9)

使用 PHP 5.3.1 測試

這篇關于在 PHP 中將字符串轉換為整數的最快方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 個表)
How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 設置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數組自動填充選擇框)
PHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 從 MSSQL-SELECT 產生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 国产成人99久久亚洲综合精品 | 国产精品久久久久久吹潮 | 国产成人影院 | 国产日韩久久久久69影院 | 成人高清网站 | 精精国产xxxx视频在线野外 | 亚洲精品黄色 | 日韩欧美在线一区 | 欧美激情第一区 | www.久久久 | 欧美午夜视频 | 亚洲网站在线 | 精品国产欧美 | 一级做a爰片性色毛片16美国 | 久久不射电影网 | 欧美a区 | 九色porny自拍视频 | 欧美精品一区二区三区四区 在线 | 蜜桃在线视频 | 水蜜桃亚洲一二三四在线 | 日韩一区二区三区四区五区 | 免费一区二区三区 | 在线观看www| 国产欧美精品区一区二区三区 | 亚洲精品视频在线看 | 日韩福利 | 国产成人精品一区 | 国内精品久久久久久久影视简单 | 国产精品久久久久久久久久久久久 | 午夜国产一区 | 丁香六月激情 | 91免费入口 | 在线 丝袜 欧美 日韩 制服 | 中文字幕在线一区 | 97伊人 | 国产精品a级 | 日韩久久久久久久 | 欧美一级一 | 国产午夜三级一区二区三 | 国外成人在线视频网站 | 久久久性|