{"id":2523,"date":"2013-08-18T21:48:15","date_gmt":"2013-08-19T03:48:15","guid":{"rendered":"http:\/\/blog.eastfist.com\/?p=2523"},"modified":"2025-11-29T21:53:04","modified_gmt":"2025-11-29T21:53:04","slug":"how-to-optimize-usage-of-qt-qrect-class","status":"publish","type":"post","link":"https:\/\/eastfist.com\/blog\/2013\/08\/18\/how-to-optimize-usage-of-qt-qrect-class\/","title":{"rendered":"How to Optimize Usage of Hundreds of Thousands of Qt QRect Class"},"content":{"rendered":"<p>If you&#8217;re planning on using hundreds of thousands of rectangles in your Qt application, you might be surprised that it might not perform at optimal speeds. Here&#8217;s how I speed up my Qt applications&#8230;<!--more--><\/p>\n<p>By Chongchen Saelee<\/p>\n<p>If you&#8217;re not already aware, I&#8217;m building my own 2D game engine, <a href=\"http:\/\/sdxm.eastfist.com\">SDXM<\/a>, which will require hundreds of thousands of rectangle structures to be processed prior to drawing their abstracted form to the screen. In my experience, I&#8217;ve found using Qt&#8217;s convenience class QRect to slow down later on in the game running process when the object count increases exponentially. This is possibly due to QRect having a bunch of convenience methods and functions, which creates an overhead per instantiated object. Imagine, it&#8217;s like going to war with a vest full of grenades, handguns, stabbing weapons, bazookas, etc., when all you&#8217;re mission is to dig a fox hole.<\/p>\n<p>So what I do is I create my own rectangle structure. (The really hip 3D programmers stopped calling a 4-variable structure rectangle a long time ago and are calling it quad now; I&#8217;m not working with 3D, so rectangle makes more sense to me)<\/p>\n<blockquote>\n<pre>\n  struct Rect{\n    int x, y;\n    int width, height;\n  };\n\n  Rect rectangle;\n<\/pre>\n<\/blockquote>\n<p>So now you have your rectangle, which you can create tons of with a QList or QVector, and you can use the [] operator to access them individually. You&#8217;ll notice that there is great improvement in your processing, especially if you&#8217;re working with hundreds and thousands of objects.<\/p>\n<p>However, you&#8217;ll have to calculate everything else by yourself, which still isn&#8217;t that big a deal because you&#8217;d only be implementing exactly the methods and functions that you&#8217;ll need.<\/p>\n<p>And note, a structure is smaller and faster than a class because it doesn&#8217;t have any functions or methods.<\/p>\n<p>I hope this helps my fellow Qt users out there!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re planning on using hundreds of thousands of rectangles in your Qt application, you might be surprised that it might not perform at optimal speeds. Here&#8217;s how I speed up my Qt applications&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,9],"tags":[43,375,784,921,1053,1233,1247,1649,1830,1845,2059,2060,2357,2484,2602,2688],"class_list":["post-2523","post","type-post","status-publish","format-standard","hentry","category-artistic","category-tech","tag-2d","tag-c","tag-draw","tag-faster","tag-game","tag-how-to","tag-hundred","tag-million","tag-optimize","tag-overhead","tag-qrect","tag-qt","tag-slow","tag-structure","tag-thousands","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/posts\/2523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/comments?post=2523"}],"version-history":[{"count":1,"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/posts\/2523\/revisions"}],"predecessor-version":[{"id":57423,"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/posts\/2523\/revisions\/57423"}],"wp:attachment":[{"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/media?parent=2523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/categories?post=2523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/tags?post=2523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}