Difference between print and stdout.printf in flushing - vala

When I run a sample in Vala-ThreadSamples:
Original version:
int question(){
for (var i = 0; i < 3; i++){
print (".");
Thread.usleep (800000);
stdout.flush ();
}
return 42;
}
void main(){
question();
}
Modified version:
int question(){
for (var i = 0; i < 3; i++){
print(".");
Thread.usleep (800000);
}
return 42;
}
void main(){
question();
}
Two version gets the same output:
print a dot and wait, which repeat three times.
Why print will flush every times?

Related

MQL4 code limiting number of open symbols

I am trying to create an expert advisor ( EA ) that would limit the number of open symbol to 3
For example, if I load the EA on 15 different symbols, it would not open a trade on more than 3 at any particular time. Below is the code I inserted in the EA but not working as I want:
int SYMBOL_NUMBER_LIMIT = 3;
string COUNTED_SYMBOLS[];
ArrayResize(COUNTED_SYMBOLS, SYMBOL_NUMBER_LIMIT, 0);
for(int s=0; s<SYMBOL_NUMBER_LIMIT; s++) COUNTED_SYMBOLS[s]="";
int SYMBOLS_IN_TRADE_SO_FAR = 0;
bool NEW_TRADE_PERMISSION = true;
int ALL_POSITIONS = OrdersTotal(); // PositionsTotal();
if(ALL_POSITIONS > 0)
{
for(int index=0; index<ALL_POSITIONS; index++)
{
string THIS_SYMBOL = OrderSymbol(); // PositionGetSymbol(index);
bool Symbol_already_counted = false;
for(int i=0; i<SYMBOL_NUMBER_LIMIT; i++)
{
if(COUNTED_SYMBOLS[i]==THIS_SYMBOL)
{
Symbol_already_counted = true;
break;
}
if(Symbol_already_counted) continue;
else
{
SYMBOLS_IN_TRADE_SO_FAR++;
if(SYMBOLS_IN_TRADE_SO_FAR >= SYMBOL_NUMBER_LIMIT)
{
NEW_TRADE_PERMISSION = false;
break;
}
for(int j=0; j<SYMBOL_NUMBER_LIMIT; j++)
if(COUNTED_SYMBOLS[j]=="")
{
COUNTED_SYMBOLS[j] = THIS_SYMBOL;
break;
}
}
}
}
}
I would appreciate any help to make it work as I described. Thanks in advance

JavaCV Display image in color from video capture

I have trouble with display image from camera. I using VideoCapture and when I try display image in grayscale it's works perfect, but when I try display image in color that I get something like that:
link
Part of my source code:
public void CaptureVideo()
{
VideoCapture videoCapture = new VideoCapture(0);
Mat frame = new Mat();
while (videoCapture.isOpened() && _canWorking)
{
videoCapture.read(frame);
if (!frame.empty())
{
Image img = MatToImage(frame);
videoView.setImage(img);
}
try { Thread.sleep(33); } catch (InterruptedException e) { e.printStackTrace(); }
}
videoCapture.release();
}
private Image MatToImage(Mat original)
{
BufferedImage image = null;
int width = original.size().width(), height = original.size().height(), channels = original.channels();
byte[] sourcePixels = MatToBytes(original, width, height, channels);
if (original.channels() > 1)
{
image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
}
else
{
image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
}
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length);
return SwingFXUtils.toFXImage(image, null);
}
private byte[] MatToBytes(Mat mat, int width, int height, int channels)
{
byte[] output = new byte[width * height * channels];
UByteRawIndexer indexer = mat.createIndexer();
int i = 0;
for (int j = 0; j < mat.rows(); j ++)
{
for (int k = 0; k < mat.cols(); k++)
{
output[i] = (byte)indexer.get(j,k);
i++;
}
}
return output;
}
Anyone can tell me what I doing wrong? I'm new in image processing and I don't get why it's not working.
Ok. I resolve this.
Solution:
byte[] output = new byte[_frame.size().width() * _frame.size().height() * _frame.channels()];
UByteRawIndexer indexer = mat.createIndexer();
int index = 0;
for (int i = 0; i < mat.rows(); i ++)
{
for (int j = 0; j < mat.cols(); j++)
{
for (int k = 0; k < mat.channels(); k++)
{
output[index] = (byte)indexer.get(i, j, k);
index++;
}
}
}
return output;

insert in simple linked list doesn't get last line

I am trying to insert into a simply linked list from a file. Do you
guys have any idea why the function doesn't insert the last line in my
file?
This is the file:
10,Ghidul Ciberbobocului,Stan Daria,3,Popescu Matei,Ionescu Gigel,Ilinca Radu,100.5
2,Spring IT,Mirodene Cristina,4,Dumitru Mihai,Vasiliu Valentin,Balasa Silvia,Dumitru Ion,400.
89,Serile teatrului studentesc,Petre Ion,2,Nicolae Ramona,Stan Alberto,1000
1,Tutoring,Petre Miruna,2,Bode Cristina,Angelescu Paul,500.5
11,IT Fest,Ciurea Ion,2,Ionescu Georgiana,Neagu Bianca,100.6
My code:
struct Proiect {
int id;
char* numeProiect;
char* numeCoordonator;
unsigned int nrStudenti;
char** studenti;
float costInscriere;
};
struct nodLista {
Proiect proiect;
nodLista *next;
};
nodLista* inserareNod(nodLista *first, Proiect p) {
nodLista* newNode = new nodLista;
newNode->next = NULL;
newNode->proiect = p;
if (!first) {
return newNode;
}
nodLista* aux = first;
while (aux->next) {
aux = aux->next;
}
aux->next = newNode;
printf("%d\n", aux->proiect.id);
return first;
}
void main() {
nodLista* first = NULL;
Proiect proiect;
FILE *f = fopen("proiecte2.txt", "r");
char line[150];
int nrProiecte = 0;
if (f) {
while (fgets(line, sizeof(line), f)) {
nrProiecte++;
}
printf("Nr proiecte: %d",nrProiecte);
fclose(f);
}
else {
printf("Fisierul nu a fost gasit");
}
f = fopen("proiecte2.txt", "r");
char *token[150], sep_list[] = ",";
Proiect* listaProiecte;
listaProiecte = (Proiect*)malloc(nrProiecte * sizeof(Proiect));
int i = 0;
if (f) {
while(fgets(line, sizeof(line), f)) {
token[0] = strtok(line, sep_list);
listaProiecte[i].id = atoi(token[0]);
first = inserareNod(first, listaProiecte[i]);
i++;
}
}
else printf("Fisierul nu aputut fi deschis");
}
Output: It only displays 10 , 2, 89 and 1.
Did you make prints inside this function to check if it's doing the while correctly?
while(fgets(line, sizeof(line), f)) {
token[0] = strtok(line, sep_list);
listaProiecte[i].id = atoi(token[0]);
first = inserareNod(first, listaProiecte[i]);
i++;
}

Why is this Dart code so slow compared to java's implementation?

The dart codes as follows is extremely slow compared to java's implementation.
//test.dart
import 'dart:io';
void main(){
for(int i = 0; i < 1 << 25;i++){
stdout.write(i); // or directly print(i);
}
stdout.close();
}
java version:
//Test.java
import java.io.*;
public class Test{
public static void main(String[]args)throws Exception {
try{
PrintWriter out = new PrintWriter(System.out);
for(int i = 0;i < 1 << 25; i++){
out.print(i);
}
out.close();
}catch(Exception e){}
}
}
$ time java Test > /dev/null
real 0m6.421s
user 0m0.046s
sys 0m0.031s
$ time dart Test.dart > /dev/null
real 0m51.978s
user 0m0.015s
sys 0m0.078s
Is stdout/print() unbuffered by default in Dart? Is there something like java's PrintWriter? thanks. (Update: after warming up vm, stdout is 2x slower than java )
real 0m15.497s
user 0m0.046s
sys 0m0.047s
===============================================================================
Update Sep 30, 2013
I have implemented custom buffers for both dart and java codes to make a further comparing,now the result is as follows:
//test.dart
final int SIZE = 8192;
final int NUM = 1 << 25;
void main(){
List<int> content = new List(SIZE);
content.fillRange(0, SIZE, 0);
for(int i = 0; i < NUM;i++){
if(i % SIZE == 0 && i > 0)
print(content);
content[i % SIZE] = i;
}
if (NUM % SIZE ==0)
print(content);
else
print(content.sublist(0, NUM % SIZE));
}
java version:
//Test.java
import java.util.Arrays;
public class Test{
public static final int SIZE = 8192;
public static final int NUM = 1 << 25;
public static void main(String[]args)throws Exception {
try{
int[] buf = new int[SIZE];
for(int i = 0;i < NUM; i++){
if(i % SIZE == 0 && i > 0)
System.out.print(Arrays.toString(buf));
buf[i % SIZE] = i;
}
if(NUM % SIZE == 0)
System.out.print(Arrays.toString(buf));
else
{
int[] newbuf = new int[NUM % SIZE];
newbuf = Arrays.copyOfRange(buf, 0, (NUM % SIZE));
System.out.print(Arrays.toString(newbuf));
}
}catch(Exception e){}
}
}
$ time java Test > /dev/null
real 0m7.397s
user 0m0.031s
sys 0m0.031s
$ time dart test.dart > /dev/null
real 0m22.406s
user 0m0.015s
sys 0m0.062s
As you see, dart is still 3x slower than java.
Maybe your code does not get optimized by the VM.
Only "frequenly" used functions are compiled and executed as native code.
Usually for such benchmark, you have to put the tested code into a function and perform a warmup. For exemple:
//test.dart
import 'dart:io';
void f(nb_shift) {
for(int i = 0; i < 1 << nb_shift;i++){
stdout.write(i); // or directly print(i);
}
}
void main(){
//warm up:
f(3);
// the test
f(25);
stdout.close();
}
Nicolas

madvise() function not working

I am trying madvise() to mark allocated memory as mergeable so that two applications having same pages can be merged.
While using the madvise() function it shows "invalid argument".
#include<stdio.h>
#include<sys/mman.h>
#include<stdlib.h>
#include<errno.h>
#define ADDR 0xf900f000
int main()
{
int *var1=NULL,*var2=NULL;
size_t size=0;
size = 1000*sizeof(int);
var1 = (int*)malloc(size);
var2 = (int *)malloc(size);
int i=0;
for(i=0;i<999;i++)
{
var1[i] = 1;
}
for(i=0;i<999;i++)
{
var2[i] = 1;
}
i = -1;
while(i<0)
{
i = madvise((void *)var1, size, MADV_MERGEABLE); //to declare mergeable
printf("%d %p\n", i, var1); //to print the output value
err(1,NULL); //to print the generated error
i = madvise((void *)var2, size, MADV_MERGEABLE); //to declare mergeable
printf("%d\n", i);
}
return 0;
}
Error:
a.out: Invalid argument
Please help me.
Thank You.
You can only merge whole pages. You can't merge arbitrary chunks of data.

Resources