Quantcast

Maximum PC

It is currently Sun Dec 21, 2014 6:28 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: Programming tricks
PostPosted: Mon May 02, 2011 5:46 pm 
8086
8086

Joined: Mon May 02, 2011 6:50 am
Posts: 51
Hello everyone!
I've started learning programming when I was 12, through some books and stuff I could scramble online. I'm really good with C now, ok with C++. Have a very good concept of loops, arrays, linked lists and recursion; bubble-sorting, recursive search through sorted list. But I can't figure out where to go next? Are there any cool techniques to use? I guess my main question is what cool things to learn in programming at intermediate level? I'm going to take two intermediate level collage classes next semester, one in VB.NET one in Java, but besides language structure, I don't think I'll learn anything new.

Also, I really want to learn more about neuro-webs (I think that's the proper English term, although I'm not sure, sorry, not a native english-speaker). Is there any good literature (preferably online) available in that topic?


Top
  Profile  
 
 Post subject: Re: Programming tricks
PostPosted: Mon May 02, 2011 6:24 pm 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Neural Nets? That is a pretty advanced topic... are you in a computer science program at college/university? I didn't take AI till my senior year!

You could start here:
viewtopic.php?f=23&t=113268


Top
  Profile  
 
 Post subject: Re: Programming tricks
PostPosted: Tue May 03, 2011 10:49 am 
7yrs+11,000 Posts
7yrs+11,000 Posts
User avatar

Joined: Tue Jul 27, 2004 5:44 pm
Posts: 11242
Location: The kitten above is not on fire.
Pointers
Classes
Inheritance
Polymorphism
Virtual Functions
Exceptions
Templates
Stacks and Queues
Binary Trees


Top
  Profile  
 
 Post subject: Re: Programming tricks
PostPosted: Thu May 05, 2011 5:05 pm 
8086
8086

Joined: Mon May 02, 2011 6:50 am
Posts: 51
Ah, yes, neural nets. That's my goal lol

gamerfreak, out of those don't know about Virtual Functions, Templates, Stacks and Ques. Binary tree sound very familiar, but I can't remember what is it exactly. Sounds like a good start, I'll go google now :D Thanks :D


Top
  Profile  
 
 Post subject: Re: Programming tricks
PostPosted: Mon May 30, 2011 7:36 pm 
Willamette
Willamette

Joined: Tue Feb 01, 2005 12:17 pm
Posts: 1493
Yes, the topics mentioned above would benefit you greatly in your study of C++. Especially linked lists, stacks, binary trees, etc, are all common things that anybody serious about C/C++ programming should know.

Are you going for a Computer Science degree? VB.Net is a good place to start for newbie programmers because the language is fairly easy to understand and learn. Java takes a bit of knowledge, although you can do basic Java with almost no programming knowledge if you follow some pre-written examples. But I feel that knowing a bit more about C++ would help you with Java, in particular object oriented programming (OOP) or classes--because Java uses them.

If your college offers them, I'd also look into maybe a course (or a few) on Data structures (binary trees, linked lists, stacks, etc). These courses can prove to be useful as well. If your really adventurous, take a course on systems design and systems programming (or maybe even on Assembler). A bit out of date, but it does offer some concepts that you may not get from using a higher level language such as C++, VB or Java.

i remember taking an assembler course in my CS degree. While the tech was outdated, it still taught me things like how to read a memory dump, how operating systems operate, and how a computer really translates and processes information from the instructions to the machine code. Although, today, with Windows being a managed system, you won't really ever have to do this type of stuff on your own (checking for memory space, allocating appropriate amounts of memory, releasing memory when done, etc); Windows manages most of that for you (unless you're writing operating systems or are not in some type of operating environment like Linux or Windows.

My only problem with VB.Net, while it is a good starter language, is that Microsoft has modified the language so much that (in my opinion) it really isn't BASIC anymore (not meant to be pun, but literally). They've made is so powerful that you can pretty much do what you do in VB.NET as you would in VC++, which originally way back when, is not how it was. Old versions of BASIC and VB were limited on what they could do, but now they are almost like dialects of eachother.


Top
  Profile  
 
 Post subject: Re: Programming tricks
PostPosted: Mon May 30, 2011 8:21 pm 
Willamette
Willamette

Joined: Mon Jan 07, 2008 2:35 pm
Posts: 1150
I'm an EE, currently going into my senior year. Two things that I really enjoyed of the (not so much, and poorly taught) programming was MIPS assembly language, and writing (Linux) Device Drivers.

Simply knowing some basic assembly language, and how a compiler works can give you a huge advantage when you write a program in a higher level language such as C or C++, since you can visualize what the code will look like once it's compiled. This coupled with a good understanding of microprocessors will allow you to write much more efficient code.

I really enjoyed writing the Linux Device Drivers since it allowed me to interface with custom hardware that I had designed and developed on an FPGA board. Actually writing a very complex device driver isn't something that I would want to do in the future perhaps, but it's difficult enough (and takes a specific background) that if you're really good at it, there can be a very high demand for you in the job market. Just make sure you stick with LINUX device drivers, as developing for Windows is just stupid back asswards and generally unfriendly (or so I've heard).


Top
  Profile  
 
 Post subject: Re: Programming tricks
PostPosted: Sun Jun 19, 2011 9:21 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
It sounds like you're ready to work through an actual algorithms and data structures book. You might want to check out one of Steve Skienna's books or go to a local college bookstore to see if something in there strikes your fancy. I highly recommend the Tardos & Kleinberg algorithms book, but it is expensive and possibly a bit too far over your head. There is an excellent algorithms course from MIT that is worth checking out -- Just skip all the computational complexity stuff for the time being.

A good way to improve your programming is to compete in TopCoder contests. They are actually quite fun, take about an hour, will uncover many holes/problems, and provide an excellent support system so that you can figure out how to improve. 100% worth it.

Almost forgot, there is a lot of good programming tasks & code on Rosetta Code. You can pick a task that looks interesting, think about how you'd implement it in your fav language and see how that compares to the version that they have on the wiki. You should also check out how to solve it in some other languages as well.

teh 1337 haxxor wrote:
Simply knowing some basic assembly language, and how a compiler works can give you a huge advantage when you write a program in a higher level language such as C or C++, since you can visualize what the code will look like once it's compiled. This coupled with a good understanding of microprocessors will allow you to write much more efficient code.

Unless you're running the compiler in something like a debug mode, I seriously doubt that the object code is going to look anything like what you'd expect (assuming you've compiled a meaningful program). Plus, the assembly code that you imagine the compiler will generate is certainly going to be much less efficient than the assembly code produced after millions, if not billions, of man-hours of compiler research and implementation. After all, if you could imagine something better or even equivalent to a compiler, you'd do the world a favor and write that compiler for us, right? =)


Top
  Profile  
 
 Post subject: Re: Programming tricks
PostPosted: Mon Jun 20, 2011 5:33 pm 
Willamette
Willamette

Joined: Mon Jan 07, 2008 2:35 pm
Posts: 1150
Example: On the GCC compiler, the -O flag can tell the compiler to optimize either for execution speed, size, or try for both.

When optimizing for execution speed, one thing the compiler may do is what's called loop unrolling. Basically, it does exactly what you think it may do.

Lets say that you're going through an array of length 10. Therefore, you use a for loop.

When you tell the compiler to compile, with not -O flag, the generated code will be some instructions (whatever you want to do to your array), with a branch instruction at the bottom that decides whether or not to branch back up and perform those instructions again, or if it's finished, then to continue.

If you were to tell the compiler to optimize for speed, then the compiler might find that no matter what the conditions are, you're going to go through this loop ten times. Therefore, since speed is most important (you told the compiler this by using the -O3 flag), the compiler will "unroll" the loop. Instead of using a branch statement, it will simply use a brute-force approach. Basically it generates every instruction that would have been executed in the above solution, and strings them together. This can greatly eliminate the number of branch instructions that are executed.

So why does this matter? Branch instructions are BAD. Why are they so bad? Because every time that the processor reaches a branch instruction, it must make a decision. Modern day processors however, have something called a pipeline. Basically, it allows for each part of the processor to be used every clock cycle. It does this by looking ahead in the code and actually load instructions ahead of the instruction currently being executed. However, when the processor comes to a branch, it has to make a decision. Which set of instructions should it start putting in the pipeline? If it guesses correctly, then there's no problem. However, when it guess wrong, it has to flush the pipeline (throwing away precious clock cycles), and start over with the correct path.

Code:
Loop:
for(int a=0; a<10; a++){
array[a]=array[a]*2;
}

Now to unroll the loop:
array[1]=array[1]*2;
array[2]=array[2]*2;
array[3]=array[3]*2;
....
array[9]=array[9]*2;

By unrolling the loop and avoiding having to execute branch instructions, we can greatly speed up the execution of an application. (However, this speedup unfortunately comes at the cost of size.) Wikipedia can probably give you a better description of loop unrolling than I can...it's been a couple semesters lol.

Basically by having a good understanding of how microprocessors operate, and how machine code works, you can gain very useful knowledge that will be a great tool to you, even when programming in very high level languages. By being able to visualize what assembly code may look like for a certain statement or group of statements, you can modify your code so that it runs much more efficiently. Knowing how your code is compiled, and then how it will run on the microprocessor that you're programming is an awesome tool!

Btw, if you want a great book to learn more about microprocessors and assembly language, consider "Computer Organization and Design: The Hardware/Software Interface" by Patterson and Hennessey. I used this book a couple semesters ago (College Junior level course in Computer Architecture), and loved it. Prof was terrible, book was amazing. I would recommend getting the one that goes over ARM instead of MIPS though, since the ARM architecture is so prevalent these days (pretty much every cellphone etc), so it will be relevant. Also, if you search ebay etc for an international edition, you can get if for like $30.


Top
  Profile  
 
 Post subject: Re: Programming tricks
PostPosted: Tue Jun 21, 2011 5:16 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
teh 1337 haxxor wrote:
Example: On the GCC compiler, the -O flag can tell the compiler to optimize either for execution speed, size, or try for both.

When optimizing for execution speed, one thing the compiler may do is what's called loop unrolling. Basically, it does exactly what you think it may do.

That is only one of probably 50 optimizations (or more) that a good compiler is going to perform. And it is one of the easiest one. Now here is the important question... how does knowing that a loop may be unrolled, or a function inlined, or some instructions completely eliminated actually help you write better code?

I could see how having a knowledge of cache lines or paging on a system may help write fast code, but I don't think anyone working a normal development job is ever going to be given time to inspect the object code produced by a compiler.

teh 1337 haxxor wrote:
Btw, if you want a great book to learn more about microprocessors and assembly language, consider "Computer Organization and Design: The Hardware/Software Interface" by Patterson and Hennessey. I used this book a couple semesters ago (College Junior level course in Computer Architecture), and loved it. Prof was terrible, book was amazing.

Awesome book... no doubt about it. You should probably REREAD the part about how compilers generate better assembly code than humans again. ;)


Top
  Profile  
 
 Post subject: Re: Programming tricks
PostPosted: Sat Sep 24, 2011 9:26 pm 
8086
8086

Joined: Mon May 02, 2011 6:50 am
Posts: 51
Actually I program microcontrollers a lot. Did a few graphics output libraries, that require speed. That's where in-lining and unrolling get useful :)


Top
  Profile  
 
 Post subject: Re: Programming tricks
PostPosted: Sun Sep 25, 2011 10:56 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24238
Location: Granite Heaven
gamerfreak wrote:
Pointers
Classes
Inheritance
Polymorphism
Virtual Functions
Exceptions
Templates
Stacks and Queues
Binary Trees


Aka, year one in CS. :)


Top
  Profile  
 
 Post subject: Re: Programming tricks
PostPosted: Mon Sep 26, 2011 12:56 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Jipstyle wrote:
gamerfreak wrote:
Pointers
Classes
Inheritance
Polymorphism
Virtual Functions
Exceptions
Templates
Stacks and Queues
Binary Trees[/quot

Aka, year one in CS. :)

Minus the harder coursework... Have you been on a siesta or what? =)


Top
  Profile  
 
 Post subject: Re: Programming tricks
PostPosted: Mon Sep 26, 2011 6:27 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24238
Location: Granite Heaven
Gadget wrote:
Jipstyle wrote:
gamerfreak wrote:
Pointers
Classes
Inheritance
Polymorphism
Virtual Functions
Exceptions
Templates
Stacks and Queues
Binary Trees[/quot

Aka, year one in CS. :)

Minus the harder coursework... Have you been on a siesta or what? =)


Nah .. quite the opposite, actually. Lots of work and not much time to post here ... especially if the posts require actual thought. ;)


Top
  Profile  
 
 Post subject: Re: Programming tricks
PostPosted: Sat Dec 24, 2011 10:16 am 
Smithfield
Smithfield

Joined: Sun Jun 18, 2006 7:37 pm
Posts: 5535
On optimization and such, I was told that for simple things like loops, don't try to optimize it yourself. The compiler will figure it out and optimize it for you. It's better to figure out other things that are causing issues with speed. For example, I was doing a side project where the microcontroller was recording timestamps and putting them in a bit-bin. I found out that it takes longer for the ISR that records the timestamps to process each one rather than process it when I have enough (and this count was always constant, or at least well defined).

I mostly program in C, but I figured out over the years (all two of them) in my job most of what you need you learn in school. But I've found some things to make my life easier:

1. pointer implementation (pimpl) lists in C. These are a life saver if you have a module that uses multiple protocols, but only one at a time, and you can group each function of those protocols into some common function name.

2. I forget what it's called, but there's a way to initialize arrays on a per element basis. e.g., if you have int foo[10] and you want to initialize the 8th element to 100, you can do it like this:
Code:
int foo[10] = { [7] = 100 };


3. Static classes in C#. I'm pretty sure C++ has the same thing.

But yeah, I find that programming is more about organization and maintainability since speed and memory isn't exactly an issue as it was years ago.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.