Unix/Linux Go Back    


What is on Your Mind? Come inside and relax a while. Discuss whatever is on your mind. New members can introduce themselves. However, technical Q&A should happen in our other forums.

Naive coding...

What is on Your Mind?


Tags
discussion, software development

Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 02-22-2017
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 27 June 2017, 2:26 PM EDT
Location: Simplicity
Posts: 3,582
Thanks: 282
Thanked 1,191 Times in 1,077 Posts
Two corrections:

Code:
case "$STR" in 
"") echo "Blank" ;;
*[!0-9]*)  echo "Contains non-numeric" ;;
*) echo "Valid" ;;
esac

The Following User Says Thank You to MadeInGermany For This Useful Post:
Corona688 (02-22-2017)
Sponsored Links
    #9  
Old Unix and Linux 03-05-2017
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 27 June 2017, 3:19 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,449
Thanks: 100
Thanked 1,510 Times in 1,120 Posts
A few observations about programming habits and programming in general:

I think the real difference between naive and non-naive (clever) programming is not so much to make (or make not) use of a certain language construct but the employment of certain algorithms. The example of Corona688 shows that very clearly. The difference in simplicity (and perhaps speed) comes not from using some "clever language feature" a non-expert might not know about, but from the ability to look at the problem from a different angle and draw the right conclusion.

Here is a story from my own practice: I once worked for a development team who created some database application. At one time they had trouble with a daily import routine because it ran approximately 32 hours. (For the non-experts: standard days come with only 24 of them. ;-) ) The import script worked like that: first a huge SQL-statement to create some table. This table was fed into a loop where something was changed, then the whole was fed back into the DB. The script looked like this (only the structure):


Code:
#! /bin/ksh

db2sql "...some 300 lines of SQL here...." |\
while read record ; do
     newrecord=$(echo $record | sed '....some changing of the record here....')
     db2sql "... import record from $newrecord here..."
done

exit 0

There is nothing "logically wrong" here and when you test it with 5 records it is probably as fast as any other solution. Doing it with some ten-million-records table, though, will reveal that calling sed one time for every record is slightly slower (by several orders of magnitude) than to call it once in a pipeline:


Code:
db2sql "...some 300 lines of SQL here...." |\
sed '....some changing of the records here....' |\
while read newrecord ; do
     db2sql "... import record from $newrecord here..."
done

So, this was my first instinctive comment and (we tested it later) it would have brought down the processing time by ~3.5 hours. But i worked on this together with the DBA and he observed inside the 300-line-SQL-monster (which i had ignored, being an SQL-ignorant) that most of it was effectively creating a left outer join of the whole database on itself and then dropping 99% of it, So, after selecting what really was to be selected - and finding out that the changes could be done inside instead of using the shell and an external program to do it - - we arrived at:


Code:
db2sql "select for update ...some 20 lines of SQL here...."

which took - about 5 seconds. All the while management was calling for bigger hardware to "meet increasing needs" instead of just firing this incompetent click-boy who confused some graphic representation of the DB within a tool (Informatica) with reality.

So, what is the differrence between clever and naive programming? It is about the same difference as 32 hours and 5 seconds.

My two cents.

bakunin
The Following 4 Users Say Thank You to bakunin For This Useful Post:
Corona688 (03-06-2017), Don Cragun (03-05-2017), ni2 (04-08-2017), zaxxon (03-06-2017)
Sponsored Links
    #10  
Old Unix and Linux 4 Weeks Ago
cb88 cb88 is offline
Registered User
 
Join Date: May 2017
Last Activity: 25 May 2017, 12:49 AM EDT
Posts: 13
Thanks: 0
Thanked 1 Time in 1 Post
I wrote a rather naive snake implementation... it worked by each position being logged in an array... so each time you move a for loop scanned the array for collisions it would take a little longer. The target machine was a 50Mhz Sparcstation LX so... I thought nothing of it when writing it but it became obvious when running the game. After about 100 indexes it would get so slow you could just about walk away and get coffee, perhaps I exaggerate a bit but still! Rather than using an array of previous positions, I probably should have allocated an array the size of the play area and marked them traversed or not.
    #11  
Old Unix and Linux 4 Weeks Ago
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 27 June 2017, 4:29 PM EDT
Location: Saskatchewan
Posts: 22,256
Thanks: 1,105
Thanked 4,200 Times in 3,885 Posts
Why was it looped? If you know where the snake is and where it's going, there's only one cell to check.
The Following User Says Thank You to Corona688 For This Useful Post:
MadeInGermany (2 Weeks Ago)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help with coding crazydude80 Shell Programming and Scripting 1 03-26-2012 06:25 PM
Naive Bayes gizmo87 Homework & Coursework Questions 0 11-15-2011 06:56 PM
can I use this coding w33man UNIX for Advanced & Expert Users 6 03-02-2004 07:49 AM



All times are GMT -4. The time now is 04:55 PM.