Solved

GetTextExtentPoint32() is slow

Posted on 2000-04-17
9
667 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 31

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
 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…

809 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