Skip to main content

All Questions

Filter by
Sorted by
Tagged with
3 votes
2 answers
79 views

Reading a file into lines (benchmarking getline() vs mmap_memchr() vs mmap_getline() vs fread())

This is a follow-up to: Reading a file into lines with mmap vs getline. The actual names of the functions are not ...
Harith's user avatar
  • 9,777
7 votes
1 answer
246 views

Reading a file into lines with mmap vs getline

I required a function to read a file into lines for a text editor, and ended up with two routines. One uses getline(), and the other uses ...
Harith's user avatar
  • 9,777
3 votes
1 answer
101 views

Simple C++ event loop - static vs dynamic dispatch performance

I have two extremely simple toy implementations of an event loop, and would like to understand the performance differences between them. First impl - events with a virtual 'handle' method - dynamic ...
Jnuk's user avatar
  • 33
1 vote
0 answers
136 views

Making Robert Tarjan's offline LCA algorithm run (much) faster (Java)

I have produced this GitHub repository that compares the performance of: Robert Tarjan's off-line lowest common ancestors algorithm, An improvement of the above algorithm. Typical demo program ...
coderodde's user avatar
  • 29.8k
3 votes
1 answer
80 views

calculating the percentage of distribution of ids per Loc for each country

Which of the two queries is theoretically better and why? I would like to understand why query 2 takes longer although there is no relevant information in the Execution Plan. I'm querying a Presto ...
Pin_Eipol's user avatar
2 votes
1 answer
162 views

C/C++ hash looking in flat database compared to PHP

I have a hash table which I would like to query as fast as possible. The hash table is stored this way : When I query a hash, I extract first 4 characters then use it to find the file. For instance a ...
James's user avatar
  • 41
1 vote
2 answers
115 views

Circular list vs. doubly-linked list: which is faster?

In this post, I will compare performance of a circular list and a conventional list with head/tail -references: ...
coderodde's user avatar
  • 29.8k
5 votes
2 answers
666 views

Recursively calculating powers by squaring

Consider the following function that implements optimised O(log n) exponentiation by squaring: ...
saxbophone's user avatar
2 votes
4 answers
1k views

Calculate the median value of combining two sorted lists

I have solved a leetcode problem (Median of Two Sorted Arrays), and I came up with two solutions. Solution 1 ...
111's user avatar
  • 45
2 votes
1 answer
154 views

LeetCode#494 target sum (dart is slower than python)

I'm trying to solve a LeetCode problem target-sum, in two languages: Python & Dart. The difference of time taken is shocking for me" Python took ~70ms while Dart took ~900ms !! Why does Dart ...
Abhishek Patil's user avatar
2 votes
1 answer
158 views

Card game simulations, with a multithreaded version slower than the single-threaded one

I'm new to rust and I'm trying to build a card game simulation to find all of the best actions with all possible hands. This requires a lot of simulations and my first 3 million are already taking a ...
PaulEliaz's user avatar
0 votes
1 answer
132 views

Find longest "binary gap" [closed]

I'm doing a few challenges and I found this one on Codility: A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the ...
GB5's user avatar
  • 101
0 votes
1 answer
71 views

2 ConvertToBase3 methods with great disparity of execution time

I have two methods to do the same thing: convert a number to ternary. ...
ludicrous's user avatar
  • 109
4 votes
3 answers
416 views

Pairwise Euclidean distance search algorithm

x and y are two-dimensional arrays with dimensions (AxN) and (BxN), i.e. they have the same number of columns. I need to get a matrix of Euclidean distances between each pair of rows from x and y. I ...
Никита Михалков's user avatar
2 votes
2 answers
856 views

Counting uppercase and lowercase characters in a string

I created two programs (one with JAVA8) to calculate the total count of uppercase and lowercase characters in given String. After checking the execution time JAVA8 is taking longer execution time than ...
user12218554's user avatar
3 votes
2 answers
472 views

Parsing custom text file using haskell

I am working on writing code for find difference between two text files that should be ideally same. the format of file is ...
lightyagami1's user avatar
3 votes
1 answer
827 views

Using Numba works slower than not using it for my Python code

I thought this community is better place to ask my question so I ask here rather than at StackOverflow. Recently, I learned that Numba can make Python function ...
Senna's user avatar
  • 41
2 votes
3 answers
223 views

Sort an array of integers with a hashset

I was trying to make a sorting algorithm for an array of integers. Here's are the steps/theory: It turns an array into a HashSet, iterates over every integer value ...
Kudos B's user avatar
  • 21
3 votes
2 answers
126 views

Is the following divide and conquer recursive algorithm for the exponentiation more efficient than the iterative one for large numbers?

I have the two following algorithms. My analysis says that both of them are \$\mathcal O(m^24^n)\$ i.e they are equivalent for big numbers (more than 32 bits). Is this right? Note that ...
asmmo's user avatar
  • 237
2 votes
2 answers
95 views

Common Unit Testing Code – Follow Up

This question is a follow up question to the Common Unit Testing Code portion of my lexical analyzer questions. My primary concern is the code in the header file and the C source file that implements ...
pacmaninbw's user avatar
  • 24.8k
7 votes
6 answers
3k views

Checking if an integer is a palindrome using either a string or a dict

I have solved the palindrome question on LeetCode today, but I wasn't happy with the original solution - I used strings, it worked slowly and used a lot of memory. So I rewrote the answer using a ...
NotAName's user avatar
  • 251
1 vote
2 answers
60 views

Looking at the hill of indents seems like this can be improved

I've spent some time on this as an answer elsewhere, and did my best to optimize it. But looking at the hill of indents seems like this can be improved. I've tried to implement an ...
user227321's user avatar
6 votes
4 answers
1k views

Computing the item price and vat for a given pricing, order, and exchange rate

How can I improve the readability of the below function using a list comprehension? Also, is there a way to improve items() performance? ...
Luis Batista's user avatar
2 votes
3 answers
309 views

MySQL/MariaDB Data extraction / String manipulation / "TopN" processing => Performance of C++ vs php vs python

EDIT1: Suspicion that mariadb++ is affecting my performance results compared to the raw "mariadb/mysql Connector-C"?? EDIT2: I did a barebones C-Only implementation and it comes in at 222ms vs the ...
Oliver Schönrock's user avatar
4 votes
1 answer
2k views

Decoding of binary data (AIS) from socket

I am currently working on a AIS message decoder written in pure Python. It's just a little project to teach me some things about binary data, decoding, etc. Because I am only programming for about a ...
M0r13n's user avatar
  • 43
2 votes
0 answers
2k views

Fast matrix transpose

I would like to improve the naive matrix transpose algorithm by using a cache friendly algorithm. So far there are 4 variants. Iterate over the source matrix: ...
Catree's user avatar
  • 121
2 votes
3 answers
3k views

Classifying integers as "weird" or "not weird"

Problem Statement Given an integer N, perform the following conditional actions: If N is odd, print Weird If N is even and in the inclusive range of 2 to 5, print Not Weird If N is even ...
Subba Lakshmi's user avatar
4 votes
1 answer
279 views

LeetCode: Symmetric Tree C#

https://leetcode.com/explore/interview/card/top-interview-questions-easy/94/trees/627 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For ...
Gilad's user avatar
  • 5,253
0 votes
1 answer
3k views

Extract HTML tags and its content from a string [closed]

Goal I want to extract HTML tags and its content from a string. The content (input) is queried from WordPress database. Sample data (input) I extract this dummy data from my WordPress database: ...
Star Light's user avatar
0 votes
2 answers
172 views

Union of two double arrays

I would like to merge two double arrays and remove from the result array approximately equal items. My two input arrays have ...
Wollmich's user avatar
  • 125
12 votes
2 answers
1k views

Calculating Fibonacci sequence in several different ways

I'm doing an experiment trying to freshen up my unit-testing and learn dynamic programming. Every test passes but I'm curious of the result of some of them and worried if I'm doing the testing ...
JsonDork's user avatar
  • 404
1 vote
1 answer
369 views

Two solutions to 2-sum

I have written a solution for two sum problem in variation. first is simple with two loops and another one is one loop and hash map. But I wonder why the simple solution is faster than the efficent ...
Manjeet Singh's user avatar
3 votes
1 answer
96 views

Most efficient way to check if user is allowed to update an object

So I have an user which can create his own Locations. I implemented a webservice in order to add and update locations. If the user is not the owner of the location i want to throw an appropriate ...
Savan Luffy's user avatar
5 votes
1 answer
608 views

Two ways of implementing a combat system

Below is a segment of a game I am writing to stay in practice with my Java. EDIT The class this method is in is the Encounter class. This class just manages the encounter between the player and an ...
Linny's user avatar
  • 10.4k
5 votes
2 answers
586 views

Optimizing Luhn check digit algorithm

The internet as a whole and Code Review in special already provide a decent amount of implementations of the Luhn check digit algorithm. They often follow a relatively "naive" strategy, in that they ...
AlexV's user avatar
  • 7,268
2 votes
2 answers
577 views

Python program for Jump game II

This is a Leetcode problem: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length ...
Justin's user avatar
  • 2,485
-3 votes
3 answers
1k views

Python program to find the first missing positive integer in a list

This is a Leetcode problem: Given an unsorted integer array, find the smallest missing positive integer. Note: Your algorithm should run in O(n) time and uses constant extra space. ...
Justin's user avatar
  • 2,485
7 votes
3 answers
4k views

malloc in main() or malloc in another function: allocating memory for a struct and its members

When initializing a struct in C, we can allocate memory inside the main function or within another function and return a pointer to the newly created struct. This ...
David's user avatar
  • 181
4 votes
1 answer
4k views

4 solutions to leetcode 202. Happy Number have almost same performance

I am working on an easy math question Happy number Happy Number - LeetCode Happy Number Write an algorithm to determine if a number is "happy". A happy number is a number defined by ...
Alice's user avatar
  • 605
3 votes
0 answers
1k views

Export EXIF data from thousands of images to CSV

I have written a script that retrieves specific fields of Exif data from thousands of images in a directory (including subdirectories) and saves the info to a csv file: ...
Hjalte's user avatar
  • 131
-3 votes
1 answer
136 views

About the problem of seeking matrix inverse

Recently I have solved the inverse matrix in three ways, found that their work efficiency has obvious differences. This is the case: ...
Shuang2019's user avatar
8 votes
4 answers
4k views

Reversing a string - two approaches

Test Case: Input- "I'm hungry!" Output- "!yrgnuh m'I" Approach 1: In this approach, I used a empty string and bind it with the input string reversely. ...
AKdeBerg's user avatar
  • 195
2 votes
2 answers
176 views

Project Euler #10 (sum of primes less than two million) in Clojure

I'm new to Clojure and using some Project Euler problems to familiarize myself with the language's capabilities. Problem #10 asks to find the sum of all primes less than two million. I wanted to ...
brian_o's user avatar
  • 839
0 votes
1 answer
515 views

Five functions to get the digits of a number

I'm writing a simple function: given a number, it will return a sequence or a collection of the digits in the number (in the correct order). i.e ...
nakiya's user avatar
  • 235
5 votes
1 answer
2k views

process data with numpy seems rather slow than pure python?

In my opinion, Vectorization operation with numpy should be much faster than use for in pure python. I write two function to get and process data in a csv file, one in numpy and another in pure python,...
user2458587's user avatar
1 vote
2 answers
55 views

Lowest open space in memory

I am writing a simple compiled programming language. Everything but the bytecode interpreter can run as slow as possible, but I would like the interpreter to fun fast, because that's why I made it ...
Totillity's user avatar
  • 111
2 votes
3 answers
132 views

Speed up weighted average for Leela Chess Zero

Problem: Given a vector of about 40 values m with normal error sd compute the weighted average of the values weighted by the ...
Oscar Smith's user avatar
  • 3,597
2 votes
1 answer
768 views

2D Matrix with jagged array isn't faster than one with a multidimensional array

I was trying to use Math.Net's dense matrix class. But, it doesn't support int. So, I have to create a wrapper for a jagged 2d array. I knew that jagged arrays ...
user avatar
3 votes
2 answers
4k views

Image resizing in Rust

Are there Rust features I could apply to optimize for simple test of a JPEG or PNG resized from 2000 x 2000 pixels to 150 x 150 pixels? ...
Brandon Clark's user avatar
4 votes
1 answer
4k views

n choose k combination in JavaScript

I was asked to solve a problem for \$\binom{n}{k}\$. I did the following implementation and am wondering if there's any feedback. Input: n = Integer ...
NinjaG's user avatar
  • 2,489