CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Aug 2006
    Posts
    140

    Does API do everything C** can do and Faster?

    Is it fair to say that the Win API contains all of the methods that the C languages contain collectively and it would be faster executing the API commands directly Vs. the methods provided by the languages?

    Does that then for all intents and purposes make the Methods practically Macros to the API?

    Cy

  2. #2
    Join Date
    Dec 2005
    Location
    England
    Posts
    86

    Re: Does API do everything C** can do and Faster?

    The Windows API builds upon the standard C API. Some C functions on Windows may be implemented in terms of the Windows API, but on the whole I would expect the reverse to be true. For the sake of portability, if a function is provided by both Windows and standard C, you should generally stick to the standard API where possible.

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Does API do everything C** can do and Faster?

    Quote Originally Posted by Cy_ View Post
    Is it fair to say that the Win API contains all of the methods that the C languages contain collectively and it would be faster executing the API commands directly Vs. the methods provided by the languages?
    Windows API is a language-independent call interface to Windows OS functionality.

    C language is an abstract and OS-independent programming language that has nothing common with OS API. A hint: the C runtime, that is taken for a kinda integral part of C language very often, is just a library that interfaces with particular OS.

    And finally, it would be absolutely fair to admit that the issues of program efficiency and performance have generally nothing to do with both.

    Does that then for all intents and purposes make the Methods practically Macros to the API?

    Cy
    It's always good to take a look at some (Visual C++ in particular) C runtime implementation to get rid of the questions of the kind. At least for some time.
    Last edited by Igor Vartanov; April 4th, 2009 at 03:48 PM.
    Best regards,
    Igor

  4. #4
    Join Date
    Aug 2006
    Posts
    140

    Re: Does API do everything C** can do and Faster?

    Well, thanks for the responses so far. Here's what raised the questions for me in the first place.

    In C#, you have StreamWriter and then, in API there's EM_STREAMOUT Message.

    Now, isn't StreamWriter a macro of sort to EM_STREAMOUT?! I haven't checked but doesn't StreamWriter end up using EM_STREAMOUT? In fact doesn't every method end up sending API messages and so utilizing the API messages directly shouldn't be more efficient?

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Does API do everything C** can do and Faster?

    Well C/C++ comparing to C# are totally different in their nature. So C#/.NET forums belong with your question about .NET guts much and much better.

    Besides, the fact that some high-level library function ends up in particular WinAPI call in a particular case never means you should throw the library out and go with plain API. Typically, you trade a little bit of performance degradation for a huge development speedup, and only very specific tasks can make you give the high-level library up.

    Another point is that you must be really good at low-level WinAPI programming to benefit from using it.

    But... you really need to get some experience in plain WinAPI to understand the high-level libraries better.
    Best regards,
    Igor

  6. #6
    Join Date
    Aug 2006
    Posts
    140

    Re: Does API do everything C** can do and Faster?

    Thanks Igor, that makes sense. The programing languages do facilitate getting the job done but I'm growing a lot of interest for the API and, like the idea of learning what goes on behind the scene as well as getting closer to the machine that could save me a few milliseconds which means a lot for my purpose.

    It's nice to confirm I wasn't on the wrong track...

    Cy

  7. #7
    Join Date
    Dec 2005
    Location
    England
    Posts
    86

    Re: Does API do everything C** can do and Faster?

    If you really need to get close to the machine then you might prefer a lower-level language, like C or FORTH, or possibly even assembly. However, I doubt that you do (although of course it's still worth it for educational purposes).

  8. #8
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Does API do everything C** can do and Faster?

    I just want to mention that switching to assembler with the purpose of getting higher performance is not so true these days anymore. The modern C++ compilers are pretty good at generating assembler and will most of the time outperform any hand coded assembler unless you are very very good at writing assembler.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Does API do everything C** can do and Faster?

    Quote Originally Posted by Cy_
    It's nice to confirm I wasn't on the wrong track...
    I would not be so confident, at least in the very particular case you mentioned.

    From MSDN:
    StreamWriter class
    Implements a TextWriter for writing characters to a stream in a particular encoding.
    EM_STREAMOUT Message

    --------------------------------------------------------------------------------

    The EM_STREAMOUT message causes a rich edit control to pass its contents to an application–defined EditStreamCallback callback function. The callback function can then write the stream of data to a file or any other location that it chooses.
    As for me, there's not a faintest relation between the two.

    Quote Originally Posted by Marc G View Post
    I just want to mention that switching to assembler with the purpose of getting higher performance is not so true these days anymore. The modern C++ compilers are pretty good at generating assembler and will most of the time outperform any hand coded assembler unless you are very very good at writing assembler.
    This is one of the points that I had in mind being saying "you must be really good at low-level WinAPI programming to benefit from using it."
    Best regards,
    Igor

  10. #10
    Join Date
    Dec 2005
    Location
    England
    Posts
    86

    Re: Does API do everything C** can do and Faster?

    I don't know about C++ — it has a fair amount of overhead stemming from the book-keeping involved in the extra features. Depends on the compiler, of course. For C that's certainly true, and for FORTH even moreso, since it's closer to the machine (and yet manages to be less cumbersome to write, hmn).

  11. #11
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Does API do everything C** can do and Faster?

    Don't underestimated the modern C++ compilers. I would encourage you to give it a try
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  12. #12
    Join Date
    Dec 2005
    Location
    England
    Posts
    86

    Re: Does API do everything C** can do and Faster?

    As I said, it does depend on the compiler I remember g++ used (a month or so ago) to be significantly behind gcc in most benchmarks, but it seems that's no longer the case. Either the g++ folks did some good work in that time or else the benchmarks are even screwier than we thought

  13. #13
    Join Date
    Nov 2007
    Posts
    613

    Re: Does API do everything C** can do and Faster?

    Quote Originally Posted by Marc G View Post
    I just want to mention that switching to assembler with the purpose of getting higher performance is not so true these days anymore. The modern C++ compilers are pretty good at generating assembler and will most of the time outperform any hand coded assembler unless you are very very good at writing assembler.
    Writing directly in assembler involves different techniques. An example, writing a loop in C requires to handle a memory location holding the loop variable, while writing it in assembler keeps the loop variable directly in the EBX register, requiring no memory access at all.
    Also the C language will need explicit comparisons to end the loop, while in the assembler language they're performed internally by the processor.

    It's a big difference. Try to process all the pixels on the screen using a C program, the program will be simply unusable. Try to do the same using assembler and you'll get the results virtually instantly.

  14. #14
    Join Date
    Dec 2005
    Location
    England
    Posts
    86

    Re: Does API do everything C** can do and Faster?

    That's not entirely true. Remember that the C code is compiled down to assembly eventually. The situation you mention is exactly the kind that a smart C compiler can easily spot and optimise to the assembly you've described.

    The difference between code generated by a modern C compiler and code written by hand is small enough that ‘simply unusable’ vs. ‘virtually instant’ will never occur in smart code. Of course, if naïve code is written, it's anybody's guess — it's possible to write stupid code in assembly that takes a hundred times longer to run than smart code in C, too.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured