Solved

GetTextExtentPoint32() is slow

Posted on 2000-04-17
9
653 Views
Last Modified: 2013-12-03
I have an app that takes a user batch and generates a dialog.  In building the display  GetTextExtentPoint32() is called to determine if the next item (button/text/combo box/custom control)will fit on the current line.  In one particularly long batch this is getting called 4043 times.  I ran MSDevStudio profiler, and it indicates that this call is the main slowdown for the display generation.

Is there a way to speed this up, or is there a more efficient way to determine the horizontal extent of a string?

I have forced the function inline with little improvement.
Thank You
0
Comment
Question by:marvinm
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 12

Accepted Solution

by:
pjknibbs earned 100 total points
ID: 2724299
GetTextExtentPoint32() does an awful lot of calculation to find the length of a string--it includes kerning between characters and other such esoteric items. Forcing it inline won't make the slightest difference because the function code is actually contained in the GDI and therefore can't physically be made inline, and it's the body of the function which is taking the time, not the call to it.

Unfortunately, your only way round this is to sacrifice the accuracy of GetTextExtentPoint32() and use an approximation. I had an application which had to format 200 pages of text; in order to make it do this quickly I wrote my own extent function which just added together all the widths of the characters making up the string: it built the character width table using GetTextExtentPoint32(), obviously, but once it had got them it worked out about 20 times faster! The small loss of accuracy wasn't noticeable for the fonts (Times, Arial, Courier) I was using for the app.
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 2724303
Oh, BTW, for a monospaced font like Courier you can just use the width of a single character and multiply by the number of characters in the string, but I'm assuming you're using a variable font like Arial or Times.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2724457
Could you explain why do you use GetTextExtentPoint32 ?

May be, DrawText or clipping will be faster.
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 2725821
Hi marvinm,

you can use GetCharWidth32() to recieve the width of each char of the font in an array which can later be used to simply calculate the length by yourself. This is much faster then GetTextExtentPoint32()

ZOPPO
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 22

Expert Comment

by:nietod
ID: 2726293
The "problem" with pjknib's and Zoppo's idea of doing the math yourself is that it doesn't take care of kerning calculations.  (Actually you could get the kerning information and use it, but then you probably woudl be running at the speed of GetTextExtentPoint32().)   If the string is reasonabley short and you can survive small innacuracies, this might not matter.  

However, it is possible that an alternative approach would be better.  What are you trying to do that you need to use GetTextExtent() so many times?
0
 
LVL 1

Author Comment

by:marvinm
ID: 2726572
The user created batch generates a display with checkboxes, text, buttons, combo boxes, and some other custom controls.  The display fits within our app.  When I generate the display, I need to know if the next item will fit on the curront line, or if I need to start a new line.  I use GetTextExtentPoint32() to determine the width of the next item to be placed and whether or not it fits on the current line.  I also use to break up large blocks of text so that as much as possible fits on the current line.
I think that the small innaccuracy involed in pjknibbs' and Zoppo's suggestion will not be a problem.  I will be working with today to see for sure.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2727048
Does your algorithm perform GetTextExtent() repeatedly in sutuations where it might not have to?  Like does it repeatedly measure the current line.  (like it might measure the line, add onto it, measure the new line again etc)  you might be able to avoid some occurances by storing some of the results from GetTextExtent()  (Allthough because of kerning, you theoretically can't add the length of two strings to get the length of the resultant string, but again that small difference might not matter.)
0
 
LVL 1

Author Comment

by:marvinm
ID: 2727091
It only calls it for the next item to add to the display.  I keep track of the current position and the window width.  So far it looks like getting the individual char widths once, and then adding up in place of GetTextExtentPoint32() will work fine and provide a substantial speed increase.
Thank You
0
 
LVL 1

Author Comment

by:marvinm
ID: 2728444
Thanks to all.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now