{"action":"created","issue":{"url":"https://api.github.com/repos/PointCloudLibrary/pcl/issues/1306","labels_url":"https://api.github.com/repos/PointCloudLibrary/pcl/issues/1306/labels{/name}","comments_url":"https://api.github.com/repos/PointCloudLibrary/pcl/issues/1306/comments","events_url":"https://api.github.com/repos/PointCloudLibrary/pcl/issues/1306/events","html_url":"https://github.com/PointCloudLibrary/pcl/issues/1306","id":103282513,"number":1306,"title":"Serious performance issues with PointCloud creation","user":{"login":"fran6co","id":726088,"avatar_url":"https://avatars.githubusercontent.com/u/726088?v=3","gravatar_id":"","url":"https://api.github.com/users/fran6co","html_url":"https://github.com/fran6co","followers_url":"https://api.github.com/users/fran6co/followers","following_url":"https://api.github.com/users/fran6co/following{/other_user}","gists_url":"https://api.github.com/users/fran6co/gists{/gist_id}","starred_url":"https://api.github.com/users/fran6co/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fran6co/subscriptions","organizations_url":"https://api.github.com/users/fran6co/orgs","repos_url":"https://api.github.com/users/fran6co/repos","events_url":"https://api.github.com/users/fran6co/events{/privacy}","received_events_url":"https://api.github.com/users/fran6co/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":24,"created_at":"2015-08-26T14:26:07Z","updated_at":"2015-08-27T06:36:19Z","closed_at":null,"body":"Hi, while profiling some code I found out that the creation of the PointCloud structure takes too much time. Digging in I found out that the problem is the std::vector for the points. For some reason std::vector and Eigen don't work that well together.\r\n\r\nI did a simple code to show this issue:\r\n\r\n**Updated with real case**\r\n```\r\nPointCloud Resize: 0.086795s\r\nPointCloud Reserve: 0.132422s\r\nArray: 0.0678629s\r\n```\r\n\r\nA simple array is 30% faster than the PointCloud. Would it be crazy to remove std::vector from the PointCloud structure?\r\n\r\n**Updated with real case**\r\n```c++\r\n#include <iostream>\r\n#include <chrono>\r\n#include <array>\r\n\r\n#include <pcl/common/common.h>\r\n\r\nconst int width = 1920, height = 1080;\r\nconst int cycles = 1000;\r\nconst int cx = width/2, cy = height/2;\r\nconst double zeroPlanePixelSize = 0.10520000010728836;\r\nconst int zeroPlaneDistance = 120;\r\n\r\nstd::array<double, 5000> zFactors;\r\n\r\nvoid pointCloudResize(uint8_t* rgb, uint16_t* depthMap)\r\n{\r\n    auto total = std::chrono::duration<double>::zero();\r\n    for (int c = 0; c < cycles; c++) {\r\n        auto start = std::chrono::high_resolution_clock::now();\r\n\r\n        {\r\n            std::vector<pcl::PointXYZRGBA> points;\r\n            points.resize(width*height);\r\n\r\n            for(int y=0, i=0;y<height;y++){\r\n                for(int x=0;x<width;x++,i++) {\r\n                    pcl::PointXYZRGBA& point = points[i];\r\n\r\n                    point.x = NAN;\r\n                    point.y = NAN;\r\n                    point.z = NAN;\r\n\r\n                    point.r = rgb[3*i];\r\n                    point.g = rgb[3*i+1];\r\n                    point.b = rgb[3*i+2];\r\n\r\n                    point.a = 255;\r\n\r\n                    uint16_t depth = depthMap[i];\r\n                    if (depth != 0) {\r\n                        double zFactor = zFactors[depth];\r\n\r\n                        point.x = static_cast<float>((x - cx) * zFactor * 0.001);\r\n                        point.y = static_cast<float>((y - cy) * zFactor * 0.001);\r\n                        point.z = static_cast<float>(depth * 0.001);\r\n\r\n                        point.a = 0;\r\n                    }\r\n                }\r\n            }\r\n        }\r\n\r\n        total += std::chrono::high_resolution_clock::now() - start;\r\n    }\r\n    std::cout << \"PointCloud Resize: \" << total.count() / cycles << \"s\" << std::endl;\r\n}\r\n\r\nvoid pointCloudReserve(uint8_t* rgb, uint16_t* depthMap)\r\n{\r\n    auto total = std::chrono::duration<double>::zero();\r\n    for (int c = 0; c < cycles; c++) {\r\n        auto start = std::chrono::high_resolution_clock::now();\r\n\r\n        {\r\n            std::vector<pcl::PointXYZRGBA> points;\r\n            points.reserve(width*height);\r\n\r\n            for(int y=0, i=0;y<height;y++){\r\n                for(int x=0;x<width;x++,i++) {\r\n                    pcl::PointXYZRGBA point;\r\n\r\n                    point.x = NAN;\r\n                    point.y = NAN;\r\n                    point.z = NAN;\r\n\r\n                    point.r = rgb[3*i];\r\n                    point.g = rgb[3*i+1];\r\n                    point.b = rgb[3*i+2];\r\n\r\n                    point.a = 255;\r\n\r\n                    uint16_t depth = depthMap[i];\r\n                    if (depth != 0) {\r\n                        double zFactor = zFactors[depth];\r\n\r\n                        point.x = static_cast<float>((x - cx) * zFactor * 0.001);\r\n                        point.y = static_cast<float>((y - cy) * zFactor * 0.001);\r\n                        point.z = static_cast<float>(depth * 0.001);\r\n\r\n                        point.a = 0;\r\n                    }\r\n\r\n                    points.push_back(point);\r\n                }\r\n            }\r\n        }\r\n\r\n        total += std::chrono::high_resolution_clock::now() - start;\r\n    }\r\n    std::cout << \"PointCloud Reserve: \" << total.count() / cycles << \"s\" << std::endl;\r\n}\r\n\r\nvoid array(uint8_t* rgb, uint16_t* depthMap)\r\n{\r\n    auto total = std::chrono::duration<double>::zero();\r\n    for (int c = 0; c < cycles; c++) {\r\n        auto start = std::chrono::high_resolution_clock::now();\r\n\r\n        {\r\n            auto points = new pcl::PointXYZRGBA[width * height];\r\n\r\n            for(int y=0, i=0;y<height;y++){\r\n                for(int x=0;x<width;x++,i++) {\r\n                    pcl::PointXYZRGBA& point = points[i];\r\n\r\n                    point.x = NAN;\r\n                    point.y = NAN;\r\n                    point.z = NAN;\r\n\r\n                    point.r = rgb[3*i];\r\n                    point.g = rgb[3*i+1];\r\n                    point.b = rgb[3*i+2];\r\n\r\n                    point.a = 255;\r\n\r\n                    uint16_t depth = depthMap[i];\r\n                    if (depth != 0) {\r\n                        double zFactor = zFactors[depth];\r\n\r\n                        point.x = static_cast<float>((x - cx) * zFactor * 0.001);\r\n                        point.y = static_cast<float>((y - cy) * zFactor * 0.001);\r\n                        point.z = static_cast<float>(depth * 0.001);\r\n\r\n                        point.a = 0;\r\n                    }\r\n                }\r\n            }\r\n\r\n            delete[] points;\r\n        }\r\n\r\n        total += std::chrono::high_resolution_clock::now() - start;\r\n    }\r\n    std::cout << \"Array: \" << total.count() / cycles << \"s\" << std::endl;\r\n}\r\n\r\nint main(int argc, const char * argv[]) {\r\n    for(int depth=0;depth<zFactors.size();depth++){\r\n        zFactors[depth] = 2 * depth * zeroPlanePixelSize / zeroPlaneDistance;\r\n    }\r\n\r\n    uint8_t * rgb = new uint8_t[width*height*3];\r\n    uint16_t * depthMap = new uint16_t[width*height];\r\n\r\n    for(int i=0;i<width*height;i++){\r\n        depthMap[i] = static_cast<uint16_t>((5000*i)/(width*height));\r\n    }\r\n\r\n    pointCloudResize(rgb, depthMap);\r\n    pointCloudReserve(rgb, depthMap);\r\n    array(rgb, depthMap);\r\n\r\n    delete [] rgb;\r\n    delete [] depthMap;\r\n\r\n    return 0;\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/PointCloudLibrary/pcl/issues/comments/135310669","html_url":"https://github.com/PointCloudLibrary/pcl/issues/1306#issuecomment-135310669","issue_url":"https://api.github.com/repos/PointCloudLibrary/pcl/issues/1306","id":135310669,"user":{"login":"soyersoyer","id":5169997,"avatar_url":"https://avatars.githubusercontent.com/u/5169997?v=3","gravatar_id":"","url":"https://api.github.com/users/soyersoyer","html_url":"https://github.com/soyersoyer","followers_url":"https://api.github.com/users/soyersoyer/followers","following_url":"https://api.github.com/users/soyersoyer/following{/other_user}","gists_url":"https://api.github.com/users/soyersoyer/gists{/gist_id}","starred_url":"https://api.github.com/users/soyersoyer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/soyersoyer/subscriptions","organizations_url":"https://api.github.com/users/soyersoyer/orgs","repos_url":"https://api.github.com/users/soyersoyer/repos","events_url":"https://api.github.com/users/soyersoyer/events{/privacy}","received_events_url":"https://api.github.com/users/soyersoyer/received_events","type":"User","site_admin":false},"created_at":"2015-08-27T06:36:18Z","updated_at":"2015-08-27T06:36:18Z","body":"Did you enable the optimalizations? (-O2, -O3)"}}