slice

Appending to slice bad performance.. why?

主宰稳场 提交于 2019-12-22 04:43:14
问题 I'm currently creating a game using GoLang. I'm measuring the FPS. I'm noticing about a 7 fps loss using a for loop to append to a slice like so: vertexInfo := Opengl.OpenGLVertexInfo{} for i := 0; i < 4; i = i + 1 { vertexInfo.Translations = append(vertexInfo.Translations, float32(s.x), float32(s.y), 0) vertexInfo.Rotations = append(vertexInfo.Rotations, 0, 0, 1, s.rot) vertexInfo.Scales = append(vertexInfo.Scales, s.xS, s.yS, 0) vertexInfo.Colors = append(vertexInfo.Colors, s.r, s.g, s.b, s

Appending to slice bad performance.. why?

断了今生、忘了曾经 提交于 2019-12-22 04:43:01
问题 I'm currently creating a game using GoLang. I'm measuring the FPS. I'm noticing about a 7 fps loss using a for loop to append to a slice like so: vertexInfo := Opengl.OpenGLVertexInfo{} for i := 0; i < 4; i = i + 1 { vertexInfo.Translations = append(vertexInfo.Translations, float32(s.x), float32(s.y), 0) vertexInfo.Rotations = append(vertexInfo.Rotations, 0, 0, 1, s.rot) vertexInfo.Scales = append(vertexInfo.Scales, s.xS, s.yS, 0) vertexInfo.Colors = append(vertexInfo.Colors, s.r, s.g, s.b, s

Why does slice [:-0] return empty list in Python

流过昼夜 提交于 2019-12-22 04:36:25
问题 Stumbled upon something slightly perplexing today while writing some unittests: blah = ['a', 'b', 'c'] blah[:-3] # [] blah[:-2] # ['a'] blah[:-1] # ['a', 'b'] blah[:-0] # [] Can't for the life of me figure out why blah[:-0] # [] should be the case, the pattern definitely seems to suggest that it should be ['a', 'b', 'c'] . Can anybody help to shed some light on why that is the case? Haven't been able to find mention in the docs as to why that is the case. 回答1: -0 is 0 , and a slice that goes

Golang: get the type of slice

こ雲淡風輕ζ 提交于 2019-12-22 03:22:58
问题 I am using reflect package to get the type of arbitrary array, but getting prog.go:17: cannot use sample_array1 (type []int) as type []interface {} in function argument [process exited with non-zero status] How do I get the type from array? I know how to get it from value. func GetTypeArray(arr []interface{}) reflect.Type { return reflect.TypeOf(arr[0]) } http://play.golang.org/p/sNw8aL0a5f 回答1: Change: GetTypeArray(arr []interface{}) to: GetTypeArray(arr interface{}) By the way, []int is not

Reverse a Python string without omitting start and end slice

谁说我不能喝 提交于 2019-12-22 01:26:45
问题 How do you reverse a Python string without omitting the start and end slice arguments? word = "hello" reversed_word = word[::-1] I understand that this works, but how would I get the result by specifying the start and end indexes? word = "hello" reversed_word = word[?:?:-1] It's hard to explain to students why word[::-1] reverses a string. It's better if I can give them logical reasoning rather than "it's the pythonic way". The way I explain word[::1] is as follows: "You have not specified

Merge Maps in Golang

老子叫甜甜 提交于 2019-12-22 01:22:51
问题 I need to merge multiple maps map1 = [ id: id_1 val: val_1 ] , map2 = [ id: id_2 val: val_2 ] and map3 = [id: id_1, val: val_3] such that the result map should be merged on the id values: result_map = [id: id_1 val: {val_1, val_3}, id: id_2 var: {val_2}} ] The code I've tried: var a = make(map[string]interface{}) for _, m := range data { for _, n := range data { if m["id"] == n["id"] { for l, k := range n { c[l] = k } } } } Is there a way this can be done? Am using Golang 1.7 Thanks 回答1:

Matlab DICOM Slices

南笙酒味 提交于 2019-12-21 19:57:01
问题 I have a DICOM image loaded as a matrix in matlab. My question is, how do I show specific slices of that image in each orthogonal direction? Like view slice x at position 100, y=0, z=0 回答1: If your matrix is M, and has d dimensions (3, or what have you) and you want to plot a 1-D "slice" of one of the dimensions then: plot(squeeze(M(n1,n2, ...,:,...)); where n1,n2,... are the positions of dimension x,y,... where you want to slice, and the operator (:) is the dimension you want to plot. for

Golang changing values of a struct inside a method of another struct

给你一囗甜甜゛ 提交于 2019-12-21 19:52:47
问题 I have an issue with structs and maybe an issue with pointers, if my guess is correct. This struct has some fields and a field that holds a slice: type Bot struct { // ... connlist []Connection } This Connection looks like this: type Connection struct { conn net.Conn messages int32 channels []string joins int32 connactive bool } My problem is changing the value of connactive to true . Bot has a method that listens to the connection: func (bot *Bot) ListenToConnection(connection Connection) {

Why does an assignment for double-sliced numpy arrays not work?

只谈情不闲聊 提交于 2019-12-21 07:58:29
问题 why do the following lines not work as I expect? import numpy as np a = np.array([0,1,2,1,1]) a[a==1][1:] = 3 print a >>> [0 1 2 1 1] # I would expect [0 1 2 3 3] Is this a 'bug' or is there another recommended way to this? On the other hand, the following works: a[a==1] = 3 print a >>> [0 3 2 3 3] Cheers, Philipp 回答1: It appears you simply can't do an assignment through a double-slice like that. This works though: a[numpy.where(a==1)[0][1:]] = 3 回答2: It's related to how fancy indexing works.

Partition a collection into “k” close-to-equal pieces (Scala, but language agnostic)

孤街浪徒 提交于 2019-12-21 03:19:56
问题 Defined before this block of code: dataset can be a Vector or List numberOfSlices is an Int denoting how many "times" to slice dataset I want to split the dataset into numberOfSlices slices, distributed as evenly as possible. By "split" I guess I mean "partition" (intersection of all should be empty, union of all should be the original) to use the set theory term, though this is not necessarily a set, just an arbitrary collection. e.g. dataset = List(1, 2, 3, 4, 5, 6, 7) numberOfSlices = 3