操作系统:windows 10
使用版本:aspose-slides-cpp-windows-22.8
使用如下代码打开附件中的文件,测量GetRect()耗时:
void PrintTextRect(System::String pptFilePath)
{
System::SharedPtrSystem::IO::FileStream stream = System::MakeObjectSystem::IO::FileStream(pptFilePath, System::IO::FileMode::Open, System::IO::FileAccess::Read, System::IO::FileShare::ReadWrite);
System::SharedPtrAspose::Slides::Presentation presentation = System::MakeObjectAspose::Slides::Presentation(stream);
System::SharedPtr<ISlideCollection> slides = presentation->get_Slides();
for (int slideIdx = 0; slideIdx < slides->get_Count(); slideIdx++)
{
System::SharedPtr<Aspose::Slides::ISlide> slide = slides->idx_get(slideIdx);
System::SharedPtr<IShapeCollection> shapes = slide->get_Shapes();
for (int i = 0, n = shapes->get_Count(); i < n; i++)
{
System::SharedPtr<IShape> shape = shapes->idx_get(i);
if (!System::ObjectExt::Is<IAutoShape>(shape))
{
continue;
}
auto autoShape = System::DynamicCast_noexcept<IAutoShape>(shape);
System::SharedPtr<Aspose::Slides::ITextFrame> textFrame = autoShape->get_TextFrame();
System::SharedPtr<Aspose::Slides::IParagraphCollection> paragraphCollection = textFrame->get_Paragraphs();
for (int paragraphIndex = 0, paragraphCount = paragraphCollection->get_Count(); paragraphIndex < paragraphCount; paragraphIndex++)
{
System::SharedPtr<Aspose::Slides::IParagraph> paragraph = paragraphCollection->idx_get(paragraphIndex);
// paragraph
auto begin1 = std::chrono::steady_clock::now();
System::Drawing::RectangleF paragraphRect = paragraph->GetRect();// 1
auto end1 = std::chrono::steady_clock::now();
auto ellapsed1 = std::chrono::duration_cast<std::chrono::milliseconds>(end1 - begin1).count();
std::cout << "Ellapsed1: " << ellapsed1 << " ms" << std::endl;
// portion
auto begin2 = std::chrono::steady_clock::now();
System::SharedPtr<Aspose::Slides::IPortionCollection> portionCollection = paragraph->get_Portions();
for (int portionIndex = 0, portionCount = portionCollection->get_Count(); portionIndex < portionCount; portionIndex++)
{
System::SharedPtr<IPortion> portion = portionCollection->idx_get(portionIndex);
System::Drawing::RectangleF portionRect = portion->GetRect();// 2
}
auto end2 = std::chrono::steady_clock::now();
auto ellapsed2 = std::chrono::duration_cast<std::chrono::milliseconds>(end2 - begin2).count();
std::cout << "Ellapsed2: " << ellapsed2 << " ms \n" << std::endl;
}
}
}
}
输出:
Ellapsed1: 2047 ms
Ellapsed2: 5963 ms
在执行paragraph->GetRect()已经计算文本位置了,再执行portion->GetRect()时是否重新计算位置了?从执行结果来看Ellapsed2>Ellapsed1。
test1.zip (315.6 KB)