How to code a header programme?

May 26, 2011 at 07:01:14
Specs: Windows XP, 1.6/1.5gbyte
Hi, is it possible to have a short programme execute before main()? It might, for example, read data from a hard disk while the code inside main() processes the same data.

Many Thanks
Bert


See More: How to code a header programme?

Report •


#1
May 26, 2011 at 07:14:28
The short (simplified) answer is "No". (There are circumstances under which "main()" might not be the first code executed, or there might even be no "main()" function at all - but they are esoteric enough that you can ignore them.)

Why not just call the function as the first line of "main()"?


Report •

#2
May 27, 2011 at 02:11:35
Hi, thanks for your reply. I though of writing a function to read the data and a second to process it. Afterwards, it struck me that two programmes, each running in their own space with an exchange of data between them, might be faster and more efficient.

Many Thanks
Bert


Report •

#3
May 27, 2011 at 03:38:24
The easiest way to do that would be to start a new thread to read the data and then let the main program process it. Obviously you need to build in safeguards to check that a particular item of data has been read before you try to process it, which causes complications. This is more efficient than spawing a new process (program), which tends to be quite expensive in resources.

Using a separate thread to read the data won't make the program any more efficient but, if it's interactive, would make the program appear more responsive. It's a technique commonly used by Windows programs (just check in Task Manager how many threads Internet Explorer, for example, runs).

If your program is more of a batch-type program (read data from file, process it, write it back to file without a lot of user interaction), then the straightforward method of doing the processes sequentially, rather than trying to run them in tandem, is almost certainly the most efficient procedure.

There's a tutorial here about programming in Windows using threads.


Report •

Related Solutions

#4
May 28, 2011 at 03:49:55
Thanks. I've written a sort programme - one million 4-byte integers into 5 megabytes in under a second and getting faster as I overcome the problems and begin to understand what is happening in RAM. That's why I thought of separating the load from the sort. I'll follow your suggestion and look at threads.
The other problem - at least in my mind - is that, as the sort gets faster, there will be less time to execute more instructions. More code might - by the end of the programme - actually slow things down!
I'm a relative beginner and code in C. I thought of using C++ but decided to code initially in C then convert later when the programme is finished. Would you recommend switching to C++ now?

Thanks, Bert


Report •

#5
May 28, 2011 at 13:58:52
I wouldn't worry too much about how many instructions there are in the program. Efficiency of the algorithm that you are using and sensible optimization are far more important.

An example of the former: You are writing a sort program. Now you could use bubble sort, but this is fairly slow. You would be better advised to use the QuickSort algorithm. I suspect that it will end up as a longer program, but it will run orders of magnitude faster.

An example of the latter: If you are writing a program that contains a loop (as most programs do) consider whether there are any instructions that could be moved outside of the loop without altering the logic. That sort of optimization can speed up a program appreciably. Another optimization is to look at any short functions in your program. If, in particular, you have any one or two line functions it will be more efficient (but less elegant) to insert those couple of lines everywhere that you would have called the function; that way you save the overhead of the function call. Good compilers can actually make these optimizations for you.

If you really are aiming for maximum speed then you need to start looking at assembler programming. But be aware that with modern processors it can be quite difficult to work out exactly how many clock cycles it takes to process a particular set of instructions. Look for any parts of the code that are traversed a lot and see if you could replace them with a bit of assembler. Profilers help here to determine which bits of a program are most often executed and so would benefit most from optimization.

As for C versus C++, in my opinion a well-written C program will almost always be quicker than a C++ program. The niceties of object orientation can be relatively expensive in processor time. But this is only important when you have a program that must execute at the maximum speed. In the real world this is seldom the case and the enhanced readability and ease of maintenance of a C++ program makes it the better choice.

My particular interest is operating systems, so I tend to use a mixture of assembler and C. But if I was writing general purpose programs I would go for C++.

I hope this makes some sense to you. Maximizing the speed of programs is quite a complicated task, and is not always the best choice. For most real-world programs ease of maintenance, by yourself or others, is far more important.


Report •

#6
May 31, 2011 at 02:44:45
Thanks ijack. You mention not using functions as they can slow the overall speed of a programme. I realised - eventually - that too.
As for using quick sort: actually, I tried that and found it to be slower than my own. Honestly. (The one million sort is down to 0.5 secs.) It is much more interesting - and, of course, more of a learning curve - to write my own code.
I'm not sure if I'll get into machine code. I've heard of it. I think I'll stick with C at the moment while coming up to speed on threads and some C++.
I think that my sort do{}while() loop is very efficient: 14 lines of code - excluding spaces. The bulk of the programme is in loading and saving etc.
Many Thanks

Report •

#7
May 31, 2011 at 05:57:27
As for C versus C++, in my opinion a well-written C program will almost always be quicker than a C++ program.
Someone's never gotten into template meta-programming, I see. :V

How To Ask Questions The Smart Way


Report •

#8
May 31, 2011 at 07:34:34
"Someone's never gotten into template meta-programming, I see."

Of course I'm familiar with template meta-programming.

I stand by what I said: a well-written C program will almost always be quicker than a C++ program. A well-written assembler program will be quicker still, but good assembler programming is quite an art and it is easy to end up with programs than execute slower than those produced by a good compiler.


Report •

#9
May 31, 2011 at 09:07:22
Yeah, I have no opinion between C and C++. I'd probably argue any speed difference is negligible on anything other than embedded systems.

I don't think anyone would argue assembly isn't the fastest option, just the worst at getting work done.

How To Ask Questions The Smart Way


Report •

#10
May 31, 2011 at 13:37:37
Just look at any mainstream Operating System. See what language it is written in. That's good enough for me.

Report •

#11
May 31, 2011 at 14:19:18
Yeah, I wouldn't trust any OS that started after 1983, either.

Not that I'm sure what you're getting at. Any heavily hit part of the kernel is going to be written in assembly by people who know what they're doing, and they're still going to take a lot longer to do it than anyone equally proficient in any other language.

How To Ask Questions The Smart Way


Report •

#12
June 1, 2011 at 00:14:41
Study the Linux sources. Very little of it is written in assembler, and it is normally where C cannot cope (loading the page registers, that sort of thing) rather than from performance considerations. C provides better run time performance than most other languages, which is why it is the language of choice for OS developers. (That is, after all, what it was designed to do.)

But, as I said earlier, in most applications C++ is a better choice as ease of maintenance is more important than out and out performance. This latter consideration is why template meta-programming is, IMO, not a good choice. It can improve performance, but at the expense of readability. If you want top performance then use a performance language rather than abusing an OO language.


Report •

#13
June 2, 2011 at 08:13:16
I'm using Codeblocks and it produces programmes that run faster than my previous compiler. Good, concise programming and a fast compiler might be ideal.
Bert228

Report •


Ask Question