I can't get this Processing.js sketch to work? - processing.js

I can't figure out why this processing sketch isn't working in some browsers. It works fine with Firefox, but it won't run in Safari and Chrome.
I am placing the code inside a script tag on my website and targeting the "canvas" as suggested on the processing website, http://processingjs.org/articles/PomaxGuide.html. At first, the sketch seemed to work fine. But once I tried using other browsers I ran into trouble. In Safari it won't load at all.
It seems like a relatively straightforward sketch so I really not sure where the problem is...
Here is the sketch:
float b = 4;
float c = 8;
int x = 10;
int k = 3;
int f = 1;
void setup() {
size(755, 685, P3D);
fill(190);
}
void draw() {
background(0, 102, 102);
translate(width/2, height/2);
strokeWeight(2);
noFill();
rect(-width/2, -height/2, 755, 685);
rect((-width/2) + 7, (-height/2) + 7, 741, 671);
noStroke();
fill(255, 204, 51);
rect((-width/2) + 40, -height/2, 20, 7);
rect((-width/2) + 120, -height/2, 20, 7);
rect((-width/2) + 200, -height/2, 20, 7);
rect((-width/2) + 280, -height/2, 20, 7);
rect((-width/2) + 360, -height/2, 20, 7);
rect((-width/2) + 440, -height/2, 20, 7);
rect((-width/2) + 520, -height/2, 20, 7);
rect((-width/2) + 600, -height/2, 20, 7);
rect((-width/2) + 680, -height/2, 20, 7);
rect((-width/2) + 40, (-height/2) + 678, 20, 7);
rect((-width/2) + 120, (-height/2) + 678, 20, 7);
rect((-width/2) + 200, (-height/2) + 678, 20, 7);
rect((-width/2) + 280, (-height/2) + 678, 20, 7);
rect((-width/2) + 360, (-height/2) + 678, 20, 7);
rect((-width/2) + 440, (-height/2) + 678, 20, 7);
rect((-width/2) + 520, (-height/2) + 678, 20, 7);
rect((-width/2) + 600, (-height/2) + 678, 20, 7);
rect((-width/2) + 680, (-height/2) + 678, 20, 7);
rect(-width/2, (-height/2) + 45, 7, 20);
rect(-width/2, (-height/2) + 127, 7, 20);
rect(-width/2, (-height/2) + 209, 7, 20);
rect(-width/2, (-height/2) + 291, 7, 20);
rect(-width/2, (-height/2) + 373, 7, 20);
rect(-width/2, (-height/2) + 445, 7, 20);
rect(-width/2, (-height/2) + 527, 7, 20);
rect(-width/2, (-height/2) + 609, 7, 20);
rect((-width/2) + 748, (-height/2) + 45, 7, 20);
rect((-width/2) + 748, (-height/2) + 127, 7, 20);
rect((-width/2) + 748, (-height/2) + 209, 7, 20);
rect((-width/2) + 748, (-height/2) + 291, 7, 20);
rect((-width/2) + 748, (-height/2) + 373, 7, 20);
rect((-width/2) + 748, (-height/2) + 445, 7, 20);
rect((-width/2) + 748, (-height/2) + 527, 7, 20);
rect((-width/2) + 748, (-height/2) + 609, 7, 20);
rotateY(map(mouseX, 0, width, -PI, PI));
//rotateX(map(mouseY, 0, height, -PI, PI));
rotateZ(map(mouseY, 0, height, -PI, PI));
//rotateX(PI/4);
//rotateZ(1);
scale(25);
stroke(255, 204, 51);
strokeWeight(1);
line(-width/2, 0, width/2, 0);
line(0, -height/2, 0, height/2);
strokeWeight(5);
point(1 + f + b, -12 + k + b, 3 + x + b);
point(1 + f + b, -13 + k + b, 3 + x + b);
point(-4 + f + b, -12 + k + b, -1 + x + b);
point(4 + f + c, -12 + k + c, 4 + x + c);
point(2 + f + c, -9 + k + c, 1 + x + c);
}
Any idea why??

works fine for me in all the browsers so far. If you stick it in a fiddle,
<script type="text/processing">
// ...your code here
</script><canvas></canvas>
like here, http://jsfiddle.net/7RGHL, and load that in your various browsers, they should all work. Since you didn't include the HTML code you used to make it run on your page, it's impossible to say what you did wrong, but your Processing code's fine, at least.

I had the same problem in chrome, it didn't work offline but it does online (don't know why), in safari you need to go to safari/preferences/advanced and activate the development menu. Once you have it you can Activate WebGL in it that way the problem should be solved.
PS: Sorry if the words in the menus are not exactly the same I have it in Spanish...

Related

Insert article price in shipment.pdf Magento 1.9

I am unable to insert the selling price of an item in the shipment pdf
I add
$page->drawText($item->getPrice(), $x + 5, $this->y, 'UTF-8');
However, I do not have the price including VAT but the price excluding VAT
How to add the ttc price or the total of the line please?
thank for this help.here is the current code :
How to adapt it to add the ttc price at the end?
foreach ($shipment->getItemsCollection() as $item) {
/**
* #var Mage_Sales_Model_Order_Shipment_Item $item
*/
$page->setFillColor(new Zend_Pdf_Color_GrayScale(0.85));
$page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
$page->setLineWidth(0.5);
$page->drawRectangle($x + 420, $this->y - 8, $page->getWidth() - 50, $this->y + 12);
$page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
$page->drawRectangle(50, $this->y - 8, $page->getWidth() - 50, $this->y + 12, Zend_Pdf_Page::SHAPE_DRAW_STROKE);
$page->drawLine($x + 70, $this->y + 12, $x + 70, $this->y - 8);
$page->drawLine($x + 350, $this->y + 12, $x + 350, $this->y - 8);
$page->drawLine($x + 420, $this->y + 12, $x + 420, $this->y - 8);
$page->drawText($item->getSku(), $x + 5, $this->y, 'UTF-8');
$page->drawText(strlen($item->getName()) > 50 ? substr($item->getName(), 0, 52) . "..." : $item->getName(), $x + 75, $this->y, 'UTF-8');
$product = Mage::getModel('catalog/product')->load($item->getProductId());
if ($product->getId()) {
$size = $product->getAttributeText('size') ?: $product->getData('size_label');
$page->drawText($size, $x + 355, $this->y, 'UTF-8');
}
$page->drawText('', $x + 425, $this->y, 'UTF-8');
$this->y -= 20;
}`**enter code here**`

How can I keep a subplot table from stretching?

How should I alter the following script to keep the subplot (on right_ from stretching? Is there a way to set either plot area of the subplot? Frustrating as I go thru the row/column sizing in the function, but when plot it just expands to fill the area. In the left subplot is the full list (22 rows). In the right I just pass half the df rows, and it fills vertically? Thx.
import pandas as pd
import matplotlib.pyplot as plt
import six
plt.rcParams['font.family'] = "Lato"
raw_data = dict(TF_001=[42, 39, 86, 15, 23, 57, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25],
SP500=[52, 41, 79, 80, 34, 47, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25],
Strategy=[62, 37, 84, 51, 67, 32, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22,
23, 24, 25],
LP_Port=[72, 43, 36, 26, 53, 88, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25])
df = pd.DataFrame(raw_data, index=pd.Index(
['Sharpe Ratio', 'Sortino Ratio', 'Calmars Ratio', 'Ulcer Index', 'Max Drawdown', 'Volatility',
'VaR', 'CVaR', 'R-Squared', 'CAGR', 'Risk-of-Ruin', 'Gain-Pain Ratio', 'Pitfall Indicator',
'Serentity Ratio', 'Common Sense Ratio', 'Kelly Criteria', 'Payoff Ratio', 'Ratio-A',
'Ratio-B', 'Ratio-C', 'Ratio-D', 'Ratio-E'], name='Metric'),
columns=pd.Index(['TF_001', 'SP500', 'Strategy', 'LP_Port'], name='Series'))
def create_table(data,
ax=None,
col_width=None,
row_height=None,
font_size=8,
header_color='#E5E5E5',
row_colors=None,
edge_color='w',
header_columns=0,
bbox=None):
if row_colors is None:
row_colors = ['#F1F8E9', 'w']
if bbox is None:
bbox = [0, 0, 1, 1]
data_table = ax.table(cellText=data.values,
colLabels=data.columns,
rowLabels=data.index,
bbox=bbox,
cellLoc='center',
rowLoc='left',
colLoc='center',
colWidths=([col_width] * len(data.columns)))
cell_map = data_table.get_celld()
for i in range(0, len(data.columns)):
cell_map[(0, i)].set_height(row_height * 0.2)
data_table.auto_set_font_size(False)
data_table.set_fontsize(font_size)
for k, cell in six.iteritems(data_table._cells):
cell.set_edgecolor(edge_color)
if k[0] == 0 or k[1] < header_columns:
cell.set_text_props(weight='heavy', color='black')
cell.set_facecolor(header_color)
else:
cell.set_facecolor(row_colors[k[0] % len(row_colors)])
for row, col in data_table._cells:
if (row == 0) or (col == -1):
data_table._cells[(row, col)].set_alpha(0.8)
return ax
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 7), constrained_layout=False)
create_table(df, ax1, col_width=1.1, row_height=0.25, font_size=8)
create_table(df.iloc[0:11, ], ax2, col_width=1.1, row_height=0.25, font_size=8)
ax1.set_title("- Conventional Risk Measures -",
fontsize=10,
fontweight='heavy',
loc='center')
ax1.axis('off')
ax2.set_title("- Second Order Risk Measures -",
fontsize=10,
fontweight='heavy',
loc='center')
ax2.axis('off')
plt.suptitle('EF QuantOne - Performance and Risk Assessment ("PaRA")',
x=0.0175,
y=0.9775,
ha='left',
fontsize=12,
weight='heavy')
plt.tight_layout()
plt.savefig('risk_parameter_table[1].pdf',
orientation='portrait',
pad_inches=0.5)
plt.show()
Figured it out ...
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import six
plt.rcParams['font.family'] = "Lato"
raw_data = dict(TF_001=[42, 39, 86, 15, 23, 57, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25],
SP500=[52, 41, 79, 80, 34, 47, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25],
Strategy=[62, 37, 84, 51, 67, 32, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22,
23, 24, 25],
LP_Port=[72, 43, 36, 26, 53, 88, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25])
df = pd.DataFrame(raw_data, index=pd.Index(
['Sharpe Ratio', 'Sortino Ratio', 'Calmars Ratio', 'Ulcer Index', 'Max Drawdown', 'Volatility',
'VaR', 'CVaR', 'R-Squared', 'CAGR', 'Risk-of-Ruin', 'Gain-Pain Ratio', 'Pitfall Indicator',
'Serentity Ratio', 'Common Sense Ratio', 'Kelly Criteria', 'Payoff Ratio', 'Ratio-A',
'Ratio-B', 'Ratio-C', 'Ratio-D', 'Ratio-E'], name='Metric'),
columns=pd.Index(['TF_001', 'SP500', 'Strategy', 'LP_Port'], name='Series'))
def create_table(data,
ax=None,
col_width=None,
row_height=None,
font_size=8,
header_color='#E5E5E5',
row_colors=None,
edge_color='w',
header_columns=0,
bbox=None):
if row_colors is None:
row_colors = ['#F1F8E9', 'w']
if bbox is None:
bbox = [0, 0, 1, 1]
data_table = ax.table(cellText=data.values,
colLabels=data.columns,
rowLabels=data.index,
bbox=bbox,
cellLoc='center',
rowLoc='left',
colLoc='center',
colWidths=([col_width] * len(data.columns)))
cell_map = data_table.get_celld()
for i in range(0, len(data.columns)):
cell_map[(0, i)].set_height(row_height * 0.2)
data_table.auto_set_font_size(False)
data_table.set_fontsize(font_size)
for k, cell in six.iteritems(data_table._cells):
cell.set_edgecolor(edge_color)
if k[0] == 0 or k[1] < header_columns:
cell.set_text_props(weight='heavy', color='black')
cell.set_facecolor(header_color)
else:
cell.set_facecolor(row_colors[k[0] % len(row_colors)])
for row, col in data_table._cells:
if (row == 0) or (col == -1):
data_table._cells[(row, col)].set_alpha(0.8)
return ax
# fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(11, 8.5), constrained_layout=False)
fig = plt.figure(figsize=(12, 10))
w, h = fig.get_size_inches()
div = np.array([w, h, w, h])
col_width = 1.1
row_height = 0.25
ax1_subplot_size = (np.array(df.shape[::-1]) + np.array([0, 1])) * np.array(
[col_width, row_height])
ax1 = fig.add_axes(np.array([1.6, 1, 4.4, 5.75]) / div)
ax2_subplot_size = (np.array(df.shape[::-1]) + np.array([0, 1])) * np.array(
[col_width, row_height])
ax2 = fig.add_axes(np.array([7.5, 3.75, 4.4, 3]) / div)
create_table(df, ax1, col_width, row_height, font_size=8)
create_table(df.iloc[0:11, ], ax2, col_width, row_height, font_size=8)
ax1.set_title("- Conventional Risk Measures -",
fontsize=10,
fontweight='heavy',
loc='center')
ax1.axis('off')
ax2.set_title("- Second Order Risk Measures -",
fontsize=10,
fontweight='heavy',
loc='center')
ax2.axis('off')
plt.suptitle('EF QuantOne - Performance and Risk Assessment ("PaRA")',
x=0.0175,
y=0.9775,
ha='left',
fontsize=12,
weight='heavy')
# plt.tight_layout()
plt.savefig('risk_parameter_table[1].pdf',
orientation='portrait',
pad_inches=0.5)
plt.show()

How to i make rectangle move across the screen processing js

I want to make the white rectangles move across the screen like traffic and stop when the light is red. I have started this on the move method, totally new to processing js.
Currently the cars are the correct distance apart but i am unsure how to move thtis loop and move across the screen until the traiff light is red.
let counter = 0;
let lightorder = 0;
//setup the enviroment i.e. roads, lines on roads and trees
void setup() {
size(1330, 720);
background(96, 153, 25);
//Roads
noStroke();
fill(100);
rect(0, 200 + 10, 1330, 300);
rect(520, 0, 300, 1500);
//Lines on road
fill(255);
rect(0, 340, 90, 30);
rect(200, 340, 90, 30);
rect(400, 340, 90, 30);
rect(860, 340, 90, 30);
rect(1060, 340, 90, 30);
rect(1260, 340, 90, 30);
rect(655, 75, 30, 90);
rect(655, 575, 30, 90);
for(int i=0; i < 10; i++){
move(i = i +20)
}
}
//work out light order and assign and fill shapes on traffic lights according
void draw() {
determineLightOrder();
trafficLight();
trafficLight2();
trafficLight3();
trafficLight4();
}
void move(int p){
rect(p, 230, 200, 70);
rect(p, 400, 200, 70);
}
//determine if traffic light is red, yellow or green and fill shape
accordingly
void trafficLight() {
let is_red_light = lightorder == 2;
let is_yellow_light = lightorder == 1;
let is_green_light = lightorder == 0;
stroke(250);
fill(0);
rect(10 + 550, 10 + 80, 40, 100);
if (is_red_light) {
fill(255, 0, 0);
}
else {
fill(100);
}
ellipse(30 + 550, 30 + 80, 20, 20);
if (is_yellow_light) {
fill(250, 250, 0);
ellipse(30 + 550, 60 + 80, 20, 20);
}
// green ligh
if (is_green_light) {
fill(0, 255, 0);
ellipse(30 + 550, 90 + 80, 20, 20);
}
}
void trafficLight2() {
//declare light color variables
let is_red_light = lightorder == 0;
let is_yellow_light = lightorder == 2;
let is_green_light = lightorder == 1;
stroke(250);
fill(0);
rect(10 + 850, 10 + 215, 40, 100);
if (is_red_light) {
fill(255, 0, 0);
}
else {
fill(100);
}
ellipse(30 + 850, 30 + 215, 20, 20);
if (is_yellow_light) {
fill(250, 250, 0);
ellipse(30 + 850, 60 + 215, 20, 20);
}
if (is_green_light) {
fill(0, 255, 0);
ellipse(30 + 850, 90 + 215, 20, 20);
}
}
//same code as the trafficLight2 different position of lights
void trafficLight3() {
stroke(250);
fill(0);
rect(10 + 420, 10 + 380, 40, 100);
let is_red_light = lightorder == 0;
let is_yellow_light = lightorder == 2;
let is_green_light = lightorder == 1;
if (is_red_light) {
fill(255, 0, 0);
}
else {
fill(100);
}
ellipse(30 + 420, 30 + 380, 20, 20);
if (is_yellow_light) {
fill(250, 250, 0);
ellipse(30 + 420, 60 + 380, 20, 20);
}
if (is_green_light) {
fill(0, 255, 0);
ellipse(30 + 420, 90 + 380, 20, 20);
}
}
//same code as trafficLight1 different position of lights
void trafficLight4() {
let is_red_light = lightorder == 2;
let is_yellow_light = lightorder == 1;
let is_green_light = lightorder == 0;
stroke(250);
fill(0);
rect(10 + 730, 10 + 525, 40, 100);
if (is_red_light) {
fill(255, 0, 0);
}
else {
fill(100);
}
ellipse(30 + 730, 30 + 525, 20, 20);
if (is_yellow_light) {
fill(250, 250, 0);
ellipse(30 + 730, 60 + 525, 20, 20);
}
if (is_green_light) {
fill(0, 255, 0);
ellipse(30 + 730, 90 + 525, 20, 20);
}
}
//determine the light order
void determineLightOrder() {
counter++;
if (counter == 200) {
counter = 0;
lightorder = 0;
}
else if (counter == 50) {
lightorder = 1;
}
else if (counter == 70) {
lightorder = 2;
}
}
Stack Overflow isn't really designed for general "how do I do this" type questions. It's for specific "I tried X, expected Y, but got Z instead" type questions. But I'll try to help in a general sense:
I wrote this tutorial on animation in Processing that I highly recommend you read. But basically, you need to store the state of your program (for example, the position of the rectangles) in a variable (or multiple variables). Use those variables to draw your scene, and then change those variables over time to create an animation.
Break your problem down into smaller pieces and take those pieces on one at a time. Can you create a simple program that shows a single moving square? Then get that square to stop given a certain criteria. Then add a second square. Keep working your way forward in small steps like that, and if you get stuck on a specific step, post a MCVE along with a more specific question. Good luck.

Performance decrease with function call

For the following function:
func CycleClock(c *ballclock.Clock) int {
for i := 0; i < fiveMinutesPerDay; i++ {
c.TickFive()
}
return 1 + CalculateBallCycle(append([]int{}, c.BallQueue...))
}
where c.BallQueue is defined as []int and CalculateBallCycle is defined as func CalculateBallCycle(s []int) int. I am having a huge performance decrease between the for loop and the return statement.
I wrote the following benchmarks to test. The first benchmarks the entire function, the second benchmarks the for loop, while the third benchmarks the CalculateBallCycle function:
func BenchmarkCycleClock(b *testing.B) {
for i := ballclock.MinBalls; i <= ballclock.MaxBalls; i++ {
j := i
b.Run("BallCount="+strconv.Itoa(i), func(b *testing.B) {
for n := 0; n < b.N; n++ {
c, _ := ballclock.NewClock(j)
CycleClock(c)
}
})
}
}
func BenchmarkCycle24(b *testing.B) {
for i := ballclock.MinBalls; i <= ballclock.MaxBalls; i++ {
j := i
b.Run("BallCount="+strconv.Itoa(i), func(b *testing.B) {
for n := 0; n < b.N; n++ {
c, _ := ballclock.NewClock(j)
for k := 0; k < fiveMinutesPerDay; k++ {
c.TickFive()
}
}
})
}
}
func BenchmarkCalculateBallCycle123(b *testing.B) {
m := []int{8, 62, 42, 87, 108, 35, 17, 6, 22, 75, 116, 112, 39, 119, 52, 60, 30, 88, 56, 36, 38, 26, 51, 31, 55, 120, 33, 99, 111, 24, 45, 21, 23, 34, 43, 41, 67, 65, 66, 85, 82, 89, 9, 25, 109, 47, 40, 0, 83, 46, 73, 13, 12, 63, 15, 90, 121, 2, 69, 53, 28, 72, 97, 3, 4, 94, 106, 61, 96, 18, 80, 74, 44, 84, 107, 98, 93, 103, 5, 91, 32, 76, 20, 68, 81, 95, 29, 27, 86, 104, 7, 64, 113, 78, 105, 58, 118, 117, 50, 70, 10, 101, 110, 19, 1, 115, 102, 71, 79, 57, 77, 122, 48, 114, 54, 37, 59, 49, 100, 11, 14, 92, 16}
for n := 0; n < b.N; n++ {
CalculateBallCycle(m)
}
}
Using 123 balls, this gives the following result:
BenchmarkCycleClock/BallCount=123-8 200 9254136 ns/op
BenchmarkCycle24/BallCount=123-8 200000 7610 ns/op
BenchmarkCalculateBallCycle123-8 3000000 456 ns/op
Looking at this, there is a huge disparity between benchmarks. I would expect that the first benchmark would take roughly ~8000 ns/op since that would be the sum of the parts.
Here is the github repository.
EDIT:
I discovered that the result from the benchmark and the result from the running program are widely different. I took what #yazgazan found and modified the benchmark function in main.go mimic somewhat the BenchmarkCalculateBallCycle123 from main_test.go:
func Benchmark() {
for i := ballclock.MinBalls; i <= ballclock.MaxBalls; i++ {
if i != 123 {
continue
}
start := time.Now()
t := CalculateBallCycle([]int{8, 62, 42, 87, 108, 35, 17, 6, 22, 75, 116, 112, 39, 119, 52, 60, 30, 88, 56, 36, 38, 26, 51, 31, 55, 120, 33, 99, 111, 24, 45, 21, 23, 34, 43, 41, 67, 65, 66, 85, 82, 89, 9, 25, 109, 47, 40, 0, 83, 46, 73, 13, 12, 63, 15, 90, 121, 2, 69, 53, 28, 72, 97, 3, 4, 94, 106, 61, 96, 18, 80, 74, 44, 84, 107, 98, 93, 103, 5, 91, 32, 76, 20, 68, 81, 95, 29, 27, 86, 104, 7, 64, 113, 78, 105, 58, 118, 117, 50, 70, 10, 101, 110, 19, 1, 115, 102, 71, 79, 57, 77, 122, 48, 114, 54, 37, 59, 49, 100, 11, 14, 92, 16})
duration := time.Since(start)
fmt.Printf("Ballclock with %v balls took %s;\n", i, duration)
}
}
This gave the output of:
Ballclock with 123 balls took 11.86748ms;
As you can see, the total time was 11.86 ms, all of which was spent in the CalculateBallCycle function. What would cause the benchmark to run in 456 ns/op while the running program runs in around 11867480 ms/op?
You wrote that CalcualteBallCycle() modifies the slice by design.
I can't speak to correctness of that approach, but it is why benchmark time of BenchmarkCalculateBallCycle123 is so different.
On first run it does the expected thing but on subsequent runs it does something completely different, because you're passing different data as input.
Benchmark this modified code:
func BenchmarkCalculateBallCycle123v2(b *testing.B) {
m := []int{8, 62, 42, 87, 108, 35, 17, 6, 22, 75, 116, 112, 39, 119, 52, 60, 30, 88, 56, 36, 38, 26, 51, 31, 55, 120, 33, 99, 111, 24, 45, 21, 23, 34, 43, 41, 67, 65, 66, 85, 82, 89, 9, 25, 109, 47, 40, 0, 83, 46, 73, 13, 12, 63, 15, 90, 121, 2, 69, 53, 28, 72, 97, 3, 4, 94, 106, 61, 96, 18, 80, 74, 44, 84, 107, 98, 93, 103, 5, 91, 32, 76, 20, 68, 81, 95, 29, 27, 86, 104, 7, 64, 113, 78, 105, 58, 118, 117, 50, 70, 10, 101, 110, 19, 1, 115, 102, 71, 79, 57, 77, 122, 48, 114, 54, 37, 59, 49, 100, 11, 14, 92, 16}
for n := 0; n < b.N; n++ {
tmp := append([]int{}, m...)
CalculateBallCycle(tmp)
}
}
This works-around this behavior by making a copy of m, so that CalculateBallCycle modifies a local copy.
The running time becomes more like the others:
BenchmarkCalculateBallCycle123-8 3000000 500 ns/op
BenchmarkCalculateBallCycle123v2-8 100 10483347 ns/op
In your CycleClock function, you are copying the c.BallQueue slice. You can improve performance significantly by using CalculateBallCycle(c.BallQueue) instead (assuming CalculateBallCycle doesn't modify the slice)
For example:
func Sum(values []int) int {
sum := 0
for _, v := range values {
sum += v
}
return sum
}
func BenchmarkNoCopy(b *testing.B) {
for n := 0; n < b.N; n++ {
Sum(m)
}
}
func BenchmarkWithCopy(b *testing.B) {
for n := 0; n < b.N; n++ {
Sum(append([]int{}, m...))
}
}
// BenchmarkNoCopy-4 20000000 73.5 ns/op
// BenchmarkWithCopy-4 5000000 306 ns/op
// PASS
There is a subtle bug in your tests.
Both methods BenchmarkCycleClock and BenchmarkCycle24 run the benchmark in a for loop, passing a closure to b.Run. Inside of those closures you initialize the clocks using the loop variable i like this:ballclock.NewClock(i).
The problem is, that all instances of your anonymous function share the same variable. And, by the time the function is run by the test runner, the loop will be finished, and all of the clocks will be initialized using the same value: ballclock.MaxBalls.
You can fix this using a local variable:
for i := ballclock.MinBalls; i <= ballclock.MaxBalls; i++ {
i := i
b.Run("BallCount="+strconv.Itoa(i), func(b *testing.B) {
for n := 0; n < b.N; n++ {
c, _ := ballclock.NewClock(i)
CycleClock(c)
}
})
}
The line i := i stores a copy of the current value of i (different for each instance of your anonymous function).

Convert ECC PKCS#8 public and private keys to traditional format

I have ECC public and private generated with BouncyCastle:
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
ECNamedCurveParameterSpec ecSpec = ECNamedCurveTable
.getParameterSpec("secp192r1");
KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC");
g.initialize(ecSpec, new SecureRandom());
KeyPair pair = g.generateKeyPair();
System.out.println(Arrays.toString(pair.getPrivate().getEncoded()));
System.out.println(Arrays.toString(pair.getPublic().getEncoded()));
byte[] privateKey = new byte[]{48, 123, 2, 1, 0, 48, 19, 6, 7, 42, -122, 72, -50, 61, 2, 1, 6, 8, 42, -122, 72, -50, 61, 3, 1, 1, 4, 97, 48, 95, 2, 1, 1, 4, 24, 14, 117, 7, -120, 15, 109, -59, -35, 72, -91, 99, -2, 51, -120, 112, -47, -1, -115, 25, 48, -104, -93, 78, -7, -96, 10, 6, 8, 42, -122, 72, -50, 61, 3, 1, 1, -95, 52, 3, 50, 0, 4, 64, 48, -104, 32, 41, 13, 1, -75, -12, -51, -24, -13, 56, 75, 19, 74, -13, 75, -82, 35, 1, -50, -93, -115, -115, -34, -81, 119, -109, -50, -39, -57, -20, -67, 65, -50, 66, -122, 96, 84, 117, -49, -101, 54, -30, 77, -110, -122}
byte[] publicKey = new byte[]{48, 73, 48, 19, 6, 7, 42, -122, 72, -50, 61, 2, 1, 6, 8, 42, -122, 72, -50, 61, 3, 1, 1, 3, 50, 0, 4, 64, 48, -104, 32, 41, 13, 1, -75, -12, -51, -24, -13, 56, 75, 19, 74, -13, 75, -82, 35, 1, -50, -93, -115, -115, -34, -81, 119, -109, -50, -39, -57, -20, -67, 65, -50, 66, -122, 96, 84, 117, -49, -101, 54, -30, 77, -110, -122}
How to convert them into traditional format which can be reused later in https://github.com/kmackay/micro-ecc/blob/master/uECC.h? I need 24 bytes private and 48 public key while now it is 125 and 75.
Gives 24 and 48, sometimes when 0 is added at the beginning 25 or 49:
ECPrivateKey ecPrivateKey = (ECPrivateKey)privateKey;
System.out.println(ecPrivateKey.getS().toByteArray().length);
ECPublicKey ecPublicKey = (ECPublicKey)publicKey;
System.out.println(ecPublicKey.getW().getAffineX().toByteArray().length + ecPublicKey.getW().getAffineY().toByteArray().length);