เพียงแวบเดียวและสำหรับตัว[]ดำเนินการมันช้ากว่าตัวชี้ดิบประมาณ 5 เท่าตามที่แสดงในโค้ดต่อไปนี้ซึ่งรวบรวมโดยใช้gcc  -lstdc++ -std=c++14 -O0และส่งออกผลลัพธ์นี้:
malloc []:     414252610                                                 
unique []  is: 2062494135                                                
uq get []  is: 238801500                                                 
uq.get()[] is: 1505169542
new is:        241049490 
ฉันกำลังเริ่มเรียนรู้ c ++ ฉันมีสิ่งนี้อยู่ในใจคุณจำเป็นต้องรู้เสมอว่าคุณกำลังทำอะไรอยู่และใช้เวลามากขึ้นเพื่อที่จะรู้ว่าคนอื่นทำอะไรใน c ++ ของคุณ
แก้ไข
ตามที่ปรุงแต่งโดย @ โมฮันกุมารฉันได้ให้รายละเอียดเพิ่มเติม เวอร์ชัน gcc คือ7.4.0 (Ubuntu 7.4.0-1ubuntu1~14.04~ppa1)ผลลัพธ์ข้างต้นได้รับเมื่อ-O0ใช้อย่างไรก็ตามเมื่อฉันใช้แฟล็ก '-O2' ฉันได้รับสิ่งนี้:
malloc []:     223
unique []  is: 105586217
uq get []  is: 71129461
uq.get()[] is: 69246502
new is:        9683
จากนั้นขยับไปclang version 3.9.0, -O0คือ:
malloc []:     409765889
unique []  is: 1351714189
uq get []  is: 256090843
uq.get()[] is: 1026846852
new is:        255421307
-O2 คือ:
malloc []:     150
unique []  is: 124
uq get []  is: 83
uq.get()[] is: 83
new is:        54
ผลของเสียงดังกราว-O2นั้นน่าทึ่งมาก
#include <memory>
#include <iostream>
#include <chrono>
#include <thread>
uint32_t n = 100000000;
void t_m(void){
    auto a  = (char*) malloc(n*sizeof(char));
    for(uint32_t i=0; i<n; i++) a[i] = 'A';
}
void t_u(void){
    auto a = std::unique_ptr<char[]>(new char[n]);
    for(uint32_t i=0; i<n; i++) a[i] = 'A';
}
void t_u2(void){
    auto a = std::unique_ptr<char[]>(new char[n]);
    auto tmp = a.get();
    for(uint32_t i=0; i<n; i++) tmp[i] = 'A';
}
void t_u3(void){
    auto a = std::unique_ptr<char[]>(new char[n]);
    for(uint32_t i=0; i<n; i++) a.get()[i] = 'A';
}
void t_new(void){
    auto a = new char[n];
    for(uint32_t i=0; i<n; i++) a[i] = 'A';
}
int main(){
    auto start = std::chrono::high_resolution_clock::now();
    t_m();
    auto end1 = std::chrono::high_resolution_clock::now();
    t_u();
    auto end2 = std::chrono::high_resolution_clock::now();
    t_u2();
    auto end3 = std::chrono::high_resolution_clock::now();
    t_u3();
    auto end4 = std::chrono::high_resolution_clock::now();
    t_new();
    auto end5 = std::chrono::high_resolution_clock::now();
    std::cout << "malloc []:     " <<  (end1 - start).count() << std::endl;
    std::cout << "unique []  is: " << (end2 - end1).count() << std::endl;
    std::cout << "uq get []  is: " << (end3 - end2).count() << std::endl;
    std::cout << "uq.get()[] is: " << (end4 - end3).count() << std::endl;
    std::cout << "new is:        " << (end5 - end4).count() << std::endl;
}