This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
notes:comporg:projects:pnc1 [2018/01/24 21:23] – [Project: IMPLEMENTATIONS AND OPTIMIZATIONS - PRIME NUMBER COMPUTATION (pnc1)] ahought2 | notes:comporg:projects:pnc1 [2018/02/06 13:41] (current) – [Project: IMPLEMENTATIONS AND OPTIMIZATIONS - PRIME NUMBER COMPUTATION (pnc1)] bstrong2 | ||
---|---|---|---|
Line 16: | Line 16: | ||
interpreter ${0}.ext ${*} | interpreter ${0}.ext ${*} | ||
+ | exit 0 | ||
+ | </ | ||
+ | |||
+ | for example: | ||
+ | < | ||
+ | #! /bin/bash | ||
+ | |||
+ | python ${0}.py ${*} | ||
exit 0 | exit 0 | ||
</ | </ | ||
Line 26: | Line 34: | ||
\\ | \\ | ||
\\ | \\ | ||
- | Pick one of the five programs you did in pnc0 and implement it in AT LEAST **5 different** programming languages \\ | + | Pick one of the five programs you did in pnc0 and implement it in AT LEAST **5 different** programming languages |
**languages currently installed on lab46: | **languages currently installed on lab46: | ||
Line 51: | Line 59: | ||
Put your **primeregbs** or whichever program you picked, and put it in each gcc directory (gccO0, gccO1, etc.), compile with the **Makefile** in each of these directories \\ | Put your **primeregbs** or whichever program you picked, and put it in each gcc directory (gccO0, gccO1, etc.), compile with the **Makefile** in each of these directories \\ | ||
- | Easy way to cp primeregbs.c from gcc directory to others | + | |
- | See man xargs for more info:\\ | + | **We deserve nice things:** To copy primeregbs.c from gcc directory to other directories |
+ | See **man xargs** for more info:\\ | ||
<cli> | <cli> | ||
- | lab46: | + | lab46: |
</ | </ | ||
+ | You will still need to run the make command. | ||
See how it runs with **pncrun** for each compiler. \\ | See how it runs with **pncrun** for each compiler. \\ | ||
+ | |||
+ | **Command Line Arguments: | ||
+ | Just a friendly reminder that when command line arguments are passed they are usually (I don't know of any cases when they aren't actually) passed as strings. Make sure to cast it from a string to whichever data type it is supposed to be. | ||
+ | |||
+ | |||
+ | **Go** \\ | ||
+ | < | ||
+ | |||
+ | **Lua** \\ | ||
+ | < | ||
+ | |||
+ | **Python3**\\ | ||
+ | I don't know for sure if this works for Python2 | ||
+ | < | ||
+ | |||
+ | |||
+ | |||
+ | **To compile a go program:** \\ | ||
+ | <cli> | ||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | This will give you an executable that you can run in the typical way for pnc programs: \\ | ||
+ | <cli> | ||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | A few tips for go: printing to stderr can be done like so: \\ | ||
+ | < | ||
+ | import " | ||
+ | import " | ||
+ | |||
+ | fmt.Fprintf(os.Stderr, | ||
+ | </ | ||
+ | |||
+ | To convert a string (such as your command line input) into a number that will prevent infinite looping later on: \\ | ||
+ | < | ||
+ | import " | ||
+ | import " | ||
+ | |||
+ | var qty int // this is how you declare an int variable called quantity | ||
+ | var err error // this is how you declare an error type variable called err | ||
+ | |||
+ | qty, err = strconv.Atoi(os.Args[1]) | ||
+ | </ | ||
+ | |||
+ | You need an error variable because this atoi function, like some things you see in python, returns two things... you can check if this error is nil or not (it should be nil if everything worked), which I recommend because if you don't use it the compiler will yell at you. Some examples of code will do something like | ||
+ | |||
+ | < | ||
+ | val, err := strconv.Atoi(a_string) | ||
+ | </ | ||
+ | |||
+ | where the := is go syntax for declaring and initializing a variable simultaneously. When I did this, however, I'd get errors later on about qty not being defined (might have something to do with defining a variable inside an if block and then trying to use it outside that block, which is a thing with lua). So, the method above works if you want to use qty later on in the program (which you // | ||
+ | |||
+ | Here's a reference for using strconv: https:// | ||
+ | |||
+ | And a reference for go in general, which I found very useful: https:// | ||
+ | |||
+ | **To run a lua program:** \\ | ||
+ | <cli> | ||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | One thing to watch out for with lua is that while it has an arg[0] value that's analogous to argv[0] in C and holds the program name, it doesn' | ||
+ | |||
+ | To write to stderr in lua: | ||
+ | < | ||
+ | |||
+ | Reference: https:// | ||
+ | |||
+ | **Timing** \\ | ||
+ | |||
+ | Thanks to a barrage of stackexchange and other internet sources which have been lost in the many, many pages I've visited, I have figured out how to do timing in python and ruby. | ||
+ | |||
+ | To do timing in python: | ||
+ | < | ||
+ | import time | ||
+ | |||
+ | start = time.time() | ||
+ | |||
+ | # prime calculation code here | ||
+ | |||
+ | end = time.time() | ||
+ | |||
+ | total = end - start | ||
+ | |||
+ | sys.stdout.write(" | ||
+ | </ | ||
+ | |||
+ | To do it in ruby: | ||
+ | < | ||
+ | require " | ||
+ | |||
+ | time = Benchmark.realtime do | ||
+ | |||
+ | // insert prime calculation code here | ||
+ | |||
+ | end | ||
+ | |||
+ | $stdout.printf " | ||
+ | </ | ||
+ | |||
+ | To do it in go: | ||
+ | < | ||
+ | import " | ||
+ | |||
+ | start := time.Now() // start time | ||
+ | |||
+ | // prime code here | ||
+ | |||
+ | end := time.Now() // end time | ||
+ | |||
+ | diff := end.Sub(start) // calculate elapsed time | ||
+ | |||
+ | fmt.Fprintf(os.Stdout, | ||
+ | </ | ||
+ | |||
+ | Reference: https:// | ||
+ | |||
+ | To do it in lua: | ||
+ | < | ||
+ | st_time = os.clock() -- start time | ||
+ | |||
+ | -- execute code here | ||
+ | |||
+ | end_time = os.clock() -- end time... note that end is verboten as a variable name in lua | ||
+ | |||
+ | io.write(string.format(" | ||
+ | </ | ||
+ | |||
+ | Code modeled after the first answer here: https:// | ||
+ | |||
+ | To do it in Java: | ||
+ | < | ||
+ | long startTime = System.nanoTime(); | ||
+ | .....your program.... | ||
+ | long endTime | ||
+ | long totalTime = endTime - startTime; | ||
+ | System.out.println(totalTime); | ||
+ | </ | ||
+ | (You can change the output format as this will give you nano seconds) | ||
+ | Or if you don't want to deal with nanoseconds you can use " | ||
+ | |||
+ | |||
+ | **How to compile and run a java program in the terminal**\\ | ||
+ | |||
+ | Because compiling a java program will cause memory issues, some boundaries are needed to compile the program: | ||
+ | < | ||
+ | javac -J-XX: | ||
+ | -J-XX: | ||
+ | </ | ||
+ | To run the program: | ||
+ | < | ||
+ | export CLASSPATH=${HOME}/ | ||
+ | |||
+ | java -XX: | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | To display the time in Bash: | ||
+ | < | ||
+ | startTime=`date +%s%N` | ||
+ | |||
+ | -Pnc1 code- | ||
+ | |||
+ | endTime=`date +%s%N` | ||
+ | |||
+ | totalTime=`bc <<< | ||
+ | totalTime=`bc <<< | ||
+ | echo " | ||
+ | </ | ||
+ | |||
+ | |||
+ | JavaScript: | ||
+ | |||
+ | The wrapper is as follows: | ||
+ | |||
+ | < | ||
+ | #!/bin/bash | ||
+ | ## | ||
+ | ## Wrapper for nodejs script | ||
+ | ## | ||
+ | nodejs ${0}.js ${*} | ||
+ | exit 0 | ||
+ | </ | ||
+ | |||
+ | To do timing in JavaScript: | ||
+ | |||
+ | < | ||
+ | var start = new Date().getTime(); | ||
+ | |||
+ | |||
+ | -Pnc1 code- | ||
+ | |||
+ | var end = new Date().getTime(); | ||
+ | |||
+ | var time = (end-start)/ | ||
+ | |||
+ | console.log(time); | ||
+ | </ | ||
+ |