LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-17-2008, 06:06 AM   #31
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928

Original Poster
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301

Quote:
Originally Posted by makyo View Post
For the performance, you may be interested in the information available at http://shootout.alioth.debian.org/ which looks at about 20 benchmark codes in 40 or so languages.
Cool site, thanks for the link, I've been looking for something like this.

The other links are also interesting, thanks.

I don't quite get this tho:
Quote:
“When the eagle flies, does it forget that its feet have touched the ground? When the tiger lands upon its prey, does it forget its moment in the air? Three pounds of VAX!”
 
Old 02-17-2008, 10:41 AM   #32
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
The nice thing about C is that it can be made as readable as the author wants it to be. It is possible to write some truly impenetrable C code, but it is also possible to write code that can be easily understood. Mostly, you use descriptive variable names, create macros with obvious names to do obvious things, and use comments.

All that said, I do understand the basic point about readability, and I don't think there is any language that satisfies the requirements set forth. If there is, I don't know what it is. The closest you will come is perhaps visual basic on Windows.
 
Old 02-17-2008, 05:48 PM   #33
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by jiml8 View Post
All that said, I do understand the basic point about readability, and I don't think there is any language that satisfies the requirements set forth. If there is, I don't know what it is. The closest you will come is perhaps visual basic on Windows.
I converted a very ugly C unit in one of my projects using a ridiculous amount of macros and now it resembles something friendly, and it's also very fast code. This used to be 3 functions and 50+ lines of ugly C (lots of strsep, strdup, and free):
Code:
INTEGRATED_DEFINE(detached_full, "request connection to a detached client", \
                                 "@[priority]@[permissions](@[local socket]...)")
{
    PROCESS_START

    STANDARD_PRIORITY_PARSE(detached_full)
    STANDARD_PERMISSION_PARSE(detached_full)

    SEND_LOOP(detached_full, connect_detached_client(CURRENT_DATA, (PARSED_PRIORITY < local_priority)? \
      local_priority : PARSED_PRIORITY, PARSED_PERMISSIONS & local_permissions, create_default))

    PROCESS_COMPLETE(detached_full)
}
Of course the macros are a separate issue, but I stacked them up to generalize the ugliness required across about 50 segments like the one above.
ta0kira

Last edited by ta0kira; 02-17-2008 at 05:54 PM.
 
Old 02-18-2008, 11:01 AM   #34
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928

Original Poster
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Interesting, well that certainly proves that it can be done, but it also proves that you have to force C to be friendly. Now the problem comes in when you have fix a bug in such code, you'll have to unravel those 50 ugly segments.

Either way, I'm not saying C is a bad language, compared to many languages it is far superior in many ways, I just have to work harder at learning it. And really, eventually I'll just have to learn it, even if I do use and try other languages, let's say something like freebasic or freepascal, I know that these languages are more restrictive than C and more often than not just get in your way. C doesn't get in your way, which I really like, but it also isn't there to stop you from stepping into a pothole ... which I hate. I guess the only way to learn is to know it well enough to avoid potholes.

Last edited by H_TeXMeX_H; 02-18-2008 at 11:03 AM.
 
Old 02-18-2008, 12:07 PM   #35
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Quote:
Originally Posted by H_TeXMeX_H View Post
C doesn't get in your way, which I really like, but it also isn't there to stop you from stepping into a pothole ... which I hate. I guess the only way to learn is to know it well enough to avoid potholes.
If you feel that way about C, then I strongly recommend C++. Keep in mind that you never *have to* use the object-oriented parts of C++. In fact, anything which is valid C code is also valid C++ code (as long as you don't name any variables 'class', etc).

C++ provides you with a fair amount more pothole-prevention than C. For example, take the string class. You don't really have to understand object orientation to use it. However, it saves you from doing lots of funky string things which will probably segfault. C++ gives you references, which are awesome at avoiding the use of pointers. The syntax for them is really simple, if you use them in their most basic sense.

Even though C++ provides these conveniences, it's always possible to go around them if they are getting in your way. Still just need to copy a large region of memory? memcpy didn't go anywhere, it's still available. Need to precisely pack the bits in a struct? Go right ahead, structs are still there. Want to recklessly use pointer arithmetic for oddly sized objects? Just cast the pointer and go ahead.

For me, C++ is much easier than C. I learned C first, and I really appreciate the new keyword, the string class, the vector class, the list class.
 
Old 02-18-2008, 12:47 PM   #36
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928

Original Poster
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
I actually learned C++ as my first language, but unfortunately they forced me to use OOP, which I didn't find useful especially for the length programs I was making (small). Basically just to test how ridiculous this was, I wrote the same program using OOP (the one they forced me to write) and without OOP (I declared no classes of any kind), the code without OOP was about 4 times smaller (and easier to follow, IMO) than the one with OOP and it did the same damn thing, and I can't say that the one written with OOP was any more readable or reusable than the one without.

But you do have a very good point, I didn't realize that C++ actually does have more 'pothole-prevention' than C. Then really, the solution might be just to use C++ instead of C, even when I don't use classes. I haven't specifically used C++ in a long time so this somehow slipped my mind that it is actually easier to use than C.

Oh, and thanks again for the suggestions, they are helpful in helping me decide what to do.

Last edited by H_TeXMeX_H; 02-18-2008 at 12:51 PM.
 
Old 02-18-2008, 03:36 PM   #37
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by ta0kira View Post
I converted a very ugly C unit in one of my projects using a ridiculous amount of macros and now it resembles something friendly, and it's also very fast code.
Here is a chunk of kernel driver code I recently wrote. Basically, this chunk of code sets up DMA data transfers between a digital signal processor on the PCI bus and the Linux host system, and it sets some flags on the DSP to provide appropriate status information for a program that runs on it (the other half of the interface driver, which actually executes the DMA transfer). With that much background, figuring this routine out should not be hard:

Code:
static void set_DMA_start(unsigned int start,unsigned int size, unsigned int dsp_start) {
	unsigned int DSPP_value,DSPP_page,*dataready;
  	DSPP_value = dsp_buf2->start+DSPP_BUF_OFFSET;
	DSPP_page = inl(DSPP_value);
  	outl(DATASTATUSREGISTERMEMPAGE,DSPP_value);
	dataready = dsp_buf0->virt_address + DATASTATUSREGISTER;
	*dataready = set_host_to_dsp_interrupt(*dataready);
	*dataready = set_dma_transfer(*dataready);
	*dataready = set_dma_data_valid(*dataready);
	dataready = dsp_buf0->virt_address + DMASTARTADDR;
	*dataready = start;
	dataready = dsp_buf0->virt_address + DSPDMASTARTADDR;
	*dataready = dsp_start;
	dataready = dsp_buf0->virt_address + DMAXFERSIZE;
	*dataready = size;
	outl(DSPP_page,DSPP_value);
}
Even C code that is doing low-level, hardware oriented things can be made perfectly readable.
 
Old 02-18-2008, 04:15 PM   #38
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by H_TeXMeX_H View Post
Interesting, well that certainly proves that it can be done, but it also proves that you have to force C to be friendly. Now the problem comes in when you have fix a bug in such code, you'll have to unravel those 50 ugly segments.
On the contrary: Although macros spread the same bug to many places, a bug fix one place will fix it many places. I had a hell of a time fixing bugs before I made the conversion I did because I'd to repeated the same pattern 100 or so times. Before it was cut-and-paste of redundant code and if I found an error in one I had to find all of the other places I copied that code to. I noticed an immediate improvement in execution and reliability once I reduced all of the redundant parts of my code using macros. It's especially helpful because I use some of the same patterns across 5+ libraries in the same project.

The 50 lines weren't directly placed into a single macro. I created small macros from simple tasks that were reused constantly, then put duplicated loops into macros using the smaller ones, then put entire procedures together using combinations of all of them, and finally the sets of functions themselves.
ta0kira
 
Old 02-18-2008, 06:02 PM   #39
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Actually, Perl has a lot of 'pothole prevention' built-in eg all vars are automatically dynamically sized, so going off the end of the buffer.
Also has references which are easier than C ptrs.
Fundamental var 'types' are

1. scalar (any single value)
2. array (zero based as per C)
3. hashes (very useful for rec structures and self-documenting if you use sensible key names)

Incidentally, you can easily create multi-level structures eg Hash-of-array-of ... etc
Easy lang to pick up if you know C, just easier to write. Does allow access to low-level stuff if you need it. Huge library of pre-written modules at search.cpan.org.
Here's the online manual with examples: http://perldoc.perl.org/
 
Old 02-18-2008, 06:07 PM   #40
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by H_TeXMeX_H View Post
Need a language that is efficient, readable, easy to use and learn
Python. It satisfy your 2nd and 3rd criteria easily. As for efficiency, it depends on how well you understand the language to use it properly. If you know C, you can extend Python to get the "efficiency" you want.
 
Old 02-18-2008, 06:34 PM   #41
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Quote:
Originally Posted by H_TeXMeX_H View Post
I actually learned C++ as my first language, but unfortunately they forced me to use OOP, which I didn't find useful especially for the length programs I was making (small).
Indeed, it seems most entry-level C++ courses are *not* truly courses to teach you C++, they are courses to teach you OOP. Whenever a program reaches a certain length/level of complexity, the object orientation will be nice, but for very small problems it's just unnecessary.

But if performance is important than C++ is almost surely your best choice. Interpreted languages will always take a performance hit over compiled languages. This hit can be minimized by clever design in the interpreter, but interpreted code will never surpass the equivalent compiled code. However, interpreted code can run faster than compiled code if it is just better code. Remember, your algorithm is almost always more important than the language.
 
Old 02-18-2008, 07:03 PM   #42
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Incidentally, for "Three pounds of VAX!", it's a play on words, specifically a Zen koan (http://en.wikipedia.org/wiki/Koan) 'Three pounds of flax!'

Re PatrickNew's comment; effectively you have 3 levels of speed:
1. truly intepreted langs eg bash (any unix shell), which re-'translates' each cmd as it is used, even in a loop (and calls a lot of external progs to do non-trivial stuff),
2. langs that 'compile on the fly to an op-tree of sorts' eg Perl (see http://www.perl.com/doc/FMTEYEWTK/comp-vs-interp.html for explanation),
3. truly compiled+linked eg C.

IME of C & Perl, the latter is almost as quick eg 80-90% of C.
As he said, it usually comes down to your algorithm & data structures, rather than the lang.
 
Old 02-18-2008, 07:54 PM   #43
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.
Quote:
Originally Posted by chrism01 View Post
Actually, Perl has a lot of 'pothole prevention' built-in eg all vars are automatically dynamically sized, so going off the end of the buffer.
Also has references which are easier than C ptrs.
Fundamental var 'types' are

1. scalar (any single value)
2. array (zero based as per C)
3. hashes (very useful for rec structures and self-documenting if you use sensible key names)

Incidentally, you can easily create multi-level structures eg Hash-of-array-of ... etc
Easy lang to pick up if you know C, just easier to write. Does allow access to low-level stuff if you need it. Huge library of pre-written modules at search.cpan.org.
Here's the online manual with examples: http://perldoc.perl.org/
What he said about built-ins, plus some external aids for better code -- I attended a short perl conference this last weekend: see some slides from state of the art and perl-novice presentations http://www.frozen-perl.org/mpw2008/wiki?node=Downloads -- back your way out of the URL to see other aspects of this conference and others that are coming up ... cheers, makyo
 
Old 02-18-2008, 08:38 PM   #44
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Quote:
Originally Posted by chrism01 View Post
Re PatrickNew's comment; effectively you have 3 levels of speed:
1. truly intepreted langs eg bash (any unix shell), which re-'translates' each cmd as it is used, even in a loop (and calls a lot of external progs to do non-trivial stuff),
2. langs that 'compile on the fly to an op-tree of sorts' eg Perl (see http://www.perl.com/doc/FMTEYEWTK/comp-vs-interp.html for explanation),
3. truly compiled+linked eg C.
Indeed, and in fact, these days it's getting even blurrier. We would mostly agree that Java qualifies as a #2, since it recompiles to native code. However, I've seen ads for embedded chips where the manufacturer went through the work of making java bytecode into the instruction format of the processor, so that java was compiled! Anyhow, it's increasingly a continuum these days, and for most standard tasks, interpreted languages of some flavor or another are very good options. "Interpreted" does not necessarily mean slow.
 
Old 02-18-2008, 08:49 PM   #45
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Quote:
Originally Posted by H_TeXMeX_H View Post
I think you might be onto something. It's true that I don't really have a style, or really have been taught any. I'll definitely try this. Is there any place that describes how to code C with a certain style. I have some textbooks on C, but they don't mention too much on style (just the basic stuff: don't write everything on one line, where to put brackets, spacing, etc.).
Always a good read: Linus' comments on coding style.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What was your first language to learn? vashsna Programming 21 06-11-2007 07:04 PM
what language is best to learn io13 Programming 4 07-09-2006 09:17 AM
What Language Should I Learn? KungFuHamster General 45 04-25-2006 02:10 PM
Which C language to learn? Heiland Programming 10 08-14-2005 08:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration