User Tools

Site Tools


notes:comporg:projects:pnc1

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
notes:comporg:projects:pnc1 [2018/01/31 16:15] – [Project: IMPLEMENTATIONS AND OPTIMIZATIONS - PRIME NUMBER COMPUTATION (pnc1)] kbeykircnotes: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
 +</code>
 +
 +for example:
 +<code>
 +#! /bin/bash
 +
 +python ${0}.py ${*}
 exit 0 exit 0
 </code> </code>
Line 61: Line 69:
  
 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** \\
 +<code> upBound, err = strconv.Atoi(os.Args[1]) </code>
 +
 +**Lua** \\
 +<code> local upBound = tonumber(arg[1])</code>
 +
 +**Python3**\\
 +I don't know for sure if this works for Python2
 +<code> upBound = int(sys.argv[1]) </code>
 +
 +
  
 **To compile a go program:** \\ **To compile a go program:** \\
Line 77: Line 101:
 import "os" import "os"
  
-fmt.Fprintf(os.Stderr, "Your did something bad and now the program must yell at you\n")+fmt.Fprintf(os.Stderr, "You did something bad and now the program must yell at you\n")
 </code> </code>
  
Line 109: Line 133:
  
 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't count this when calculating the number of arguments provided. In a way, this makes it "smarter" than C, but watch out... if you want to check for a number of arguments, you can use #arg (which returns the number of command line arguments provided), but if you want one command line argument, it's #arg == 1, NOT #arg == 2. If you've got argument checking in place it'll start spitting out errors telling you you don't have enough arguments when you actually do. 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't count this when calculating the number of arguments provided. In a way, this makes it "smarter" than C, but watch out... if you want to check for a number of arguments, you can use #arg (which returns the number of command line arguments provided), but if you want one command line argument, it's #arg == 1, NOT #arg == 2. If you've got argument checking in place it'll start spitting out errors telling you you don't have enough arguments when you actually do.
 +
 +To write to stderr in lua:
 +<code> io.stderr:write() </code>
 +
 +Reference: https://www.lua.org/pil/21.2.html
 +
 +**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:
 +<code>
 +import time
 +
 +start = time.time()
 +
 +# prime calculation code here
 +
 +end = time.time()
 +
 +total = end - start
 +
 +sys.stdout.write("%8.4lf\n" % total)
 +</code>
 +
 +To do it in ruby:
 +<code>
 +require "benchmark"
 +
 +time = Benchmark.realtime do
 +
 +// insert prime calculation code here
 +
 +end
 +
 +$stdout.printf "%8.4f\n" time
 +</code>
 +
 +To do it in go:
 +<code>
 +import "time"
 +
 +start := time.Now() // start time
 +
 +// prime code here
 +
 +end := time.Now() // end time
 +
 +diff := end.Sub(start) // calculate elapsed time
 +
 +fmt.Fprintf(os.Stdout, "%8.4f\n", diff.Seconds())
 +</code>
 +
 +Reference: https://golang.org/pkg/time/ 
 +
 +To do it in lua:
 +<code>
 +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("%8.4f\n", end_time - st_time))
 +</code>
 +
 +Code modeled after the first answer here: https://stackoverflow.com/questions/24015241/timing-a-function-tasks-in-lua?lq=1
 +
 +To do it in Java:
 +<code>
 +long startTime = System.nanoTime();
 +.....your program....
 +long endTime   = System.nanoTime();
 +long totalTime = endTime - startTime;
 +System.out.println(totalTime);
 +</code>
 +(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 "System.currentTimeMillis()" instead
 +
 +
 +**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:
 +<code>
 +javac -J-XX:MaxHeapSize=512M -J-XX:InitialHeapSize=256M
 +-J-XX:CompressedClassSpaceSize=128M -J-XX:MaxMetaspaceSize=128m -J-XX:MetaspaceSize=64m primeregbs.java
 +</code>
 +To run the program:
 +<code>
 +export CLASSPATH=${HOME}/src/comporg/pnc1/java
 +
 +java -XX:MaxHeapSize=512M -XX:InitialHeapSize=256M -XX:CompressedClassSpaceSize=128M -XX:MaxMetaspaceSize=128m -XX:MetaspaceSize=64m primeregbs ${*}
 +</code>
 +
 +
 +
 +To display the time in Bash:
 +<code>
 +startTime=`date +%s%N`
 +
 +-Pnc1 code-
 +
 +endTime=`date +%s%N`
 +
 +totalTime=`bc <<< "scale=5; ${endTime} - ${startTime}"`
 +totalTime=`bc <<< "scale=4; ${totalTime} / 1000000000"`
 +echo "$totalTime"
 +</code>
 +
 +
 +JavaScript:
 +
 +The wrapper is as follows:
 +
 +<code>
 +#!/bin/bash
 +##
 +## Wrapper for nodejs script
 +##
 +nodejs ${0}.js ${*}
 +exit 0
 +</code>
 +
 +To do timing in JavaScript:
 +
 +<code>
 +var start = new Date().getTime();
 +
 +
 +-Pnc1 code-
 +
 +var end = new Date().getTime();
 +
 +var time = (end-start)/1000;
 +
 +console.log(time);
 +</code>
 +
notes/comporg/projects/pnc1.1517415307.txt.gz · Last modified: 2018/01/31 16:15 by kbeykirc