php 如何创建uuid
传统的创建uuid的方法是自己写个函数实现随机
<?php
function create_uuid($prefix="") {
$chars = md5(uniqid(mt_rand(), true));
$uuid = substr ( $chars, 0, 8 ) . '-'
. substr ( $chars, 8, 4 ) . '-'
. substr ( $chars, 12, 4 ) . '-'
. substr ( $chars, 16, 4 ) . '-'
. substr ( $chars, 20, 12 );
return $prefix.$uuid ;
} $uuid = create_uuid();
var_dump($uuid);
不过现在有扩展了 就使用扩展吧
使用 apt search uuid 搜索一下
sudo apt search uuid
得到结果

然后就安装吧
使用命令
sudo apt-get install php7.4-uuid
安装
$ sudo apt-get install php7.4-uuid
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'php-uuid' instead of 'php7.4-uuid'
The following NEW packages will be installed:
php-uuid
0 upgraded, 1 newly installed, 0 to remove and 95 not upgraded.
Need to get 8,520 B of archives.
After this operation, 51.2 kB of additional disk space will be used.
Get:1 http://cn.archive.ubuntu.com/ubuntu focal/universe amd64 php-uuid amd64 1.1.0-1build1 [8,520 B]
Fetched 8,520 B in 1s (14.4 kB/s)
Selecting previously unselected package php-uuid.
(Reading database ... 222525 files and directories currently installed.)
Preparing to unpack .../php-uuid_1.1.0-1build1_amd64.deb ...
Unpacking php-uuid (1.1.0-1build1) ...
Setting up php-uuid (1.1.0-1build1) ...
Processing triggers for libapache2-mod-php7.4 (7.4.3-4ubuntu2.4) ...
Processing triggers for php7.4-cli (7.4.3-4ubuntu2.4) ...
可以看到此时扩展已经有了
$ php -m |grep uuid
uuid
web 端需要 sudo /etc/init.d/apache2 restart 一下
然后 phpinfo()

之后使用php 函数 uuid_create 即可。
<?php $uuid = uuid_create(1);
var_dump($uuid);
cli 端得到
string(36) "a84f0c4e-a24b-11eb-87fd-9f2061574580"
web 端得到

经过测试发现还是扩展的方式实现更快。
传统方式实现uuid 100万次
代码
<?php
function create_uuid($prefix="") {
$chars = md5(uniqid(mt_rand(), true));
$uuid = substr ( $chars, 0, 8 ) . '-'
. substr ( $chars, 8, 4 ) . '-'
. substr ( $chars, 12, 4 ) . '-'
. substr ( $chars, 16, 4 ) . '-'
. substr ( $chars, 20, 12 );
return $prefix.$uuid ;
} $start_time = microtime(true);
for ($i=0; $i < 1000000; $i++) {
$uuid = create_uuid(1);
}
$end_time = microtime(true);
var_dump(($end_time-$start_time));
exit;
效果

扩展实现
<?php
$start_time = microtime(true);
for ($i=0; $i < 1000000; $i++) {
$uuid = uuid_create(1);
}
$end_time = microtime(true);
var_dump(($end_time-$start_time));
exit;

效率几乎提升一倍啊!看来还是扩展厉害啊,毕竟扩展都底层语言了嘛。
不过单次执行一次来看时间几乎可以忽略不计
传统方式得到的时间是 4.9829483032227E-5
扩展方式得到的时间是 2.6941299438477E-5
但是单次的这个耗时对比极不稳定,有时候传统方式用时反而短,这个扩展有时候时间也用的长,这说明单次其实两者并没有太大差异。而如果这个东西用到了百次 、万次循环的时候,它使用扩展就很有用了。
php 如何创建uuid的更多相关文章
- iOS创建UUID
- (NSString *)getUUID { CFUUIDRef uuidObj = CFUUIDCreate(nil); //create a new UUID NSString * uuidSt ...
- c++ 创建 uuid guid
如果没安装,先安装: [root@localhost]# yum install libuuid-devel #include "uuid/uuid.h" 引用 libuuid.s ...
- [转帖]瀚高数据库创建uuid的方法
使用syssso登录,并执行下列语句 highgo=> select set_secure_level('off'); set_secure_level -------------------- ...
- UUID的具体用法
String deleteUuid = UUID.randomUUID().toString(); java.util 类 UUID java.lang.Object java.util.UUID 所 ...
- java 的UUID的具体用法
参照JDK public final class UUIDextends Objectimplements Serializable, Comparable<UUID> 表示通用唯一标识符 ...
- Java生成唯一GUID UUID
GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值. GUID ...
- 一个UUID生成算法的C语言实现 --- WIN32版本 .
一个UUID生成算法的C语言实现——WIN32版本 cheungmine 2007-9-16 根据定义,UUID(Universally Unique IDentifier,也称GUID)在时 ...
- 获取设备的UUID
很多时候需要获取设备的UUID,比如在蓝牙交互时,需要获取服务和特征的UUID,那么如何获取设备的UUID呢?请见如下代码: // // ViewController.m // 获取UUID // / ...
- boost uuid
uuid: uuid库是一个小的使用工具,可以表示和生成UUID UUID是University Unique Identifier的缩写,它是一个128位的数字(16字节),不需要有一个中央认证机构 ...
- Python——uuid
uuid模块在Python 2.5以后引入,接口包括:不可变对象UUID(UUID类)和函数uuid1().uuid3().uuid4()和uuid5(),后面的四个函数用于生成 RFC 4122 规 ...
随机推荐
- 英文短句“xxx for the rest of us”的意思
"xxx for the rest of us" 这个短语通常被理解为"为我们所有人"或"为我们剩下的人".为了更好地理解这个短语的意义,我 ...
- Chrome扩展插件的开发--获取网页Cookies
Chrome扩展插件的开发--获取网页Cookies Chrome浏览器在浏览器类应用软件中一直居于榜首,很多人选择Chrome浏览器不仅仅是因为它的稳定,还有它丰富的可拓展性.那么有没有想自己开发一 ...
- Android RecyclerView 获取当前滚动到的Item项
背景:RecyclerView 左右滑动时,需要获取当前显示在页面上的选项卡 步骤: 1. RecyclerView 添加addOnScrollListener,回调中可以直接获取对应Item I ...
- SpringMVC的视图
目录 ThymeleafView 转发视图 重定向视图 视图控制器view-controller SpringMVC中的视图是View接口,视图的作用渲染数据,将模型Model中的数据展示给用户Spr ...
- 新手入门 | 搭建 AI 模型开发环境
目录 安装显卡驱动和开发库 对于 Tesla 系列显卡 对于 N 卡 安装 CUDA 和 cuDNN 安装 Miniconda 安装 PyTorch 和 Transformers 使用 Modelsc ...
- Java是值传递还是引用传递,又是怎么体现的
关于Java是值传递还是引用传递,可以从代码层面来实现一下拿到结果 执行下面的代码: public static void main(String[] args) { int num = 10; St ...
- TypeScript – Get Started
前言 我学 TypeScript 的时候是 Angular 2.0 beta 的年代... 现在 Angular 都快 14 了. 但由于已经有 1 年半没有写 Angular 和 TypeScrip ...
- 墨天轮沙龙 | Proxima 刘方:阿里巴巴大规模向量检索实时服务化引擎 Proxima SE
导读 随着 AI 技术的广泛应用,以及数据规模的不断增长,向量检索也逐渐成了 AI 技术链路中不可或缺的一环. 在11月16日举办的[墨天轮数据库沙龙-向量数据库专场]邀请到阿里巴巴高级技术专家刘方, ...
- 04 Transformer 中的位置编码的 Pytorch 实现
1:10 点赞 16:00 我爱你 你爱我 1401 class PositionalEncoding(nn.Module): def __init__(self, dim, dropout, max ...
- .NET程序获取当前IP经纬度,并通过经纬度实现天气查询功能
创建一个.net 8的webapi项目备用 编辑一个实体类,该实体类用于存储获取ip地址的经纬度数据使用 继续编辑三个类,用来存储对应经纬度的具体天气数据包: 改造默认的天气控制器,里 ...