Quick question about types...

This is a discussion on Quick question about types... within the C++ Programming forums, part of the General Programming Boards category; Dear sir, I apologize if I misrepresented the intention of your example (for example, I don't have a good idea ...

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Dear sir, I apologize if I misrepresented the intention of your example (for example, I don't have a good idea what is do_inherit that you are using in inheriting)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Obviously, if you don't know the maximum bound at compile time, you'll have to allocate dynamically.

    However, given that your maximum number of elements is 7, how about simply overallocating? The LLVM project has a SmallVector<typename T, unsigned N> template that provides the same interface as std::vector, but will contain an internal array of N elements, and only allocate dynamically if its size exceeds that. (A small buffer optimization.) You could write something very similar very easily.

    The easiest way of implementing it is actually by writing a sbo_allocator, or even a fixed_allocator.
    Last edited by CornedBee; 12-05-2008 at 09:54 AM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    I'm afraid that I'll access garbage data or so. I have a pretty speedy implementation already, but... I can't seem to profile and get any good results. Hmph.
    CodeAnalyst doesn't give me any source...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,165
    I apologize if I misrepresented the intention of your example
    ^_^

    That's just it. I don't know if you did or not.

    This 'non-templated base_bounds_array' followed by 'base_bounds_array<int>* arr' and later 'Yeah, let's keep it simple.' leaves me wondering if I'm still sane this morning.

    Soma

  5. #20
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Ah, I meant remove the non-type template parameters from the base type (that are causing the problems).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Quote Originally Posted by phantomotap View Post
    ^_^

    That's just it. I don't know if you did or not.

    This 'non-templated base_bounds_array' followed by 'base_bounds_array<int>* arr' and later 'Yeah, let's keep it simple.' leaves me wondering if I'm still sane this morning.

    Soma
    Sane? Of course.
    I have used a similar technique before. It's pretty handy.
    But it required a few more code changes than the alternative, so I thought I'd just avoid it.
    Great idea and technique. You do produce examples that typically breaks down any walls the language might put up.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,165
    Sane? Of course.
    The fact that my knuckles are bleeding because I washed my hands so much yesterday strongly suggests otherwise.

    And, I know at least two PhDs and an MD who'd disagree. ^_^

    Anyway, I wasn't following what anon said, but he clarified the misstep I wasn't getting. It's all good.

    Soma

  8. #23
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    I agree with the vector approach. Besides, since it already does it's own bounds checking, you don't have to (just be sure to inline your functions). I also noticed that you're using int's to represent the bounds - wouldn't size_t be more appropriate?
    Code:
    #include <ip.hpp>

  9. #24
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    I'm just letting the compiler do its job of inlining what it feels is necessary. It's in a single source file, as well, so any compiler should be able to do it.
    I don't really care about the size_t ordeal and how the standard library uses it. I feel int is more than enough. It would be a simple change later anyway, if I must.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    >> I'm just letting the compiler do its job of inlining what it feels is necessary. It's in a single source file, as well, so any compiler should be able to do it.

    Right, but just keep in mind that indexing elements is sometimes a source of bottlenecks, and that's why I always recommend aggressive inlining.

    >> I don't really care about the size_t ordeal and how the standard library uses it. I feel int is more than enough. It would be a simple change later anyway, if I must.

    well, size_t is just a better fit, being an unsigned integer. But not a huge deal, of course.
    Code:
    #include <ip.hpp>

  11. #26
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Hrm, now if only I could get the profiler working... I have never done this for a DLL loaded by another application before.
    Actually, I recoded some macro code in VBA to C++ because VBA was so disgustingly slow.
    Everything works, but... no matter what I do, I can't get any profile information.
    Using profiling in Visual Studio makes excel bark at not being able to find the DLL. CodeAnalyst refuses to me show me any source for my module after the profile duration...
    So very strange.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    So let's change this discussion a little from types to optimization. I finally got the profiler to work and the biggest bottleneck is PointsForLevel function.
    This is how it looks:

    Code:
    int PointsForLevel(Worksheet Sheet, int Country, int Level)
    {
    	int Points = 0;
    	CComVBString Addr;
    	for (int i = Level; i >= 1; i--)
    	{
    		Addr = AppendInt<CComVBString>(g_KeyPointsLvlCol[i], Country);
    		Points += (int)g_GetRange(Sheet, Addr);
    	}
    	return Points;
    }
    The biggest thieves are the two red lines.
    The first line appends an integer to a string and returns it as a VB String object (as the code should say).
    The second makes a callback to VBA to get the value stored in the cell whose address is stored in Addr (in the Sheet specified).

    AppendInt delegates calls to other template functions, which all look like this:
    Code:
    // Internal functions
    template<typename ReturnType, typename Str1, typename Str2> ReturnType AppendString(const Str1& str1, const Str2& str2)
    {
    	ReturnType RetStr = str1;
    	RetStr += str2;
    	return RetStr;
    }
    template<typename ReturnType, typename Str2> ReturnType AppendString(const std::wstring& str1,
    	const Str2& str2) { return AppendString<ReturnType>(str1.c_str(), str2); }
    template<typename ReturnType, typename Str1> ReturnType AppendString(const Str1& str1,
    	const std::wstring& str2) { return AppendString<ReturnType>(str1, str2.c_str()); }
    template<typename ReturnType> ReturnType AppendString(const std::wstring& str1,
    	const std::wstring& str2) { return AppendString<ReturnType>(str1.c_str(), str2.c_str()); }
    
    template<typename ReturnType, typename Str2> ReturnType AppendInt(const int& int1,
    	const Str2& str2) { return AppendString<ReturnType>(boost::lexical_cast<std::wstring>(int1), str2); }
    template<typename ReturnType, typename Str1> ReturnType AppendInt(const Str1& str1,
    	const int& int2) { return AppendString<ReturnType>(str1, boost::lexical_cast<std::wstring>(int2)); }
    The biggest drawback, from what I gather, is boost::lexical_cast.

    So, now the question becomes, how do I optimize this?
    For the first line, I suppose mostly that I need to use another solution than boost::lexical_cast. But what?
    It's the first step. I don't know if the delegating itself is a performance problem.

    For the second line, it seems I must avoid doing the callback itself. That probably means I must cache the values in the cells and let VBA notify the DLL when any of them changes.
    But I'm also asking myself it is truly necessary. Perhaps I do not need the speed, so at least, I shall focus on line 1.

    Any suggestions?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You mean the first line has any significance whatsoever next to the VBA call in the second?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #29
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    They're spread 50/50.
    Although it may be futile to try to optimize it seeing how little time is spent inside the DLL and elsewhere (hint: 0.00&#37; XD)...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Sounds like wasted effort then.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  2. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  3. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  4. Quick Question Regarding Variable Types
    By Drek in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2002, 12:16 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21