|
|
Noting from the design or even architecture perspective anytime code is going to be 'waiting' on something else (thread, process, Rest, message, etc) then one should at least consider what would happen if it never happens.
So for example that is why one should consider timeouts. Doesn't mean you should implement that but one needs to at least put some thought into the impact on the application/enterprise if nothing every happens.
|
|
|
|
|
Is there a way to add all the elements of a vector to an integer sequentially?
So...the vector has 1, 2, and 3
The integer is 10
The result should be 11, 12, and 13
As a side note, is there a way to add a repeating number to an integer to infinity?
Example...the integer is 10 and I want to add 4 to it. Then 4 again. And again. And again in a loop
|
|
|
|
|
puckettrobinson675 wrote: is there a way to add a repeating number to an integer to infinity?
Example...the integer is 10 and I want to add 4 to it. Then 4 again. And again. And again in a loop
Yes, just add 4 in the loop. however, not infinitely but until your integer exceeds the possible nax value for its type.
See C and C++ Integer Limits | Microsoft Learn
|
|
|
|
|
Something like:
int number = 10;
std::vector vec = { 1, 2, 3 };
for (int value : vec)
{
std::cout << number + value << std::endl;
}
In the second case you need to add a check that the sum does not overflow beyond the maximum range of the integer.
|
|
|
|
|
Quote: Is there a way to add all the elements of a vector to an integer sequentially? Yes, there are, at least, three different ways:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void dump (vector<int> v)
{
for (auto x :v )
cout << x << " ";
cout << "\n";
}
int main()
{
{ vector<int> v{1,2,3};
int c = 4;
for (size_t n = 0; n<v.size(); ++n)
{
v[n] += c;
}
cout << "method 1: ";
dump(v);
}
{ vector<int> v{1,2,3};
int c = 4;
for (auto & x : v)
{
x += c;
}
cout << "method 2: ";
dump(v);
}
{ vector<int> v{1,2,3};
int c = 4;
transform(v.begin(), v.end(), v.begin(), [=](int x){ return x+c; });
cout << "method 3: ";
dump(v);
}
}
Quote: As a side note, is there a way to add a repeating number to an integer to infinity? Yes, but this way the addition will overflow. Try:
#include <iostream>
using namespace std;
int main()
{
int i = 1;
int last_i;
int c = 4;
for (;;)
{
last_i = i;
i += c;
if ( i < last_i)
break;
}
cout << "unfortunately, (" << last_i << "+" << c << ") makes " << i;
}
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
What is the difference between two images looking the same, if one uses compression and the other one doesn’t. For example we have images made of 12 pixels with all pixels having the same color, except the first three which are different then the rest. If we consider a color depth of 24 bit the bitmap version will have 12*24 bit, 288 bit total. In the compressed image the first 3 pixels will weight 24 bit and the other ones another 24 bit, 48 total. The start and end pixels marking a color sequence need to be considered too, that’s index 0, index 2, index 3 and index 11. Is that correct?
|
|
|
|
|
Probably not - it depends on the compression method.
There are two different types of image compression: lossless and lossy.
PNG for example is lossless - when it is decompressed to a bitmap for display, the bitmap is identical to the original input bitmap data.
JPG is lossy - when it is decompressed the resulting image is lower quality than the original.
You can prove this with any image editor: load a bitmap, save it as a JPG. Open the JPG, save it as a new JPG. Repeat a few times, and watch how the image size drops, the compare the original with the final result. It doesn't take many iterations before the result as very clear to see.
Do the same with a PNG file and the result will be identical to the original.
There is also the problem that any form of compression adds overhead to the resulting file to manage the compression - and small files or those containing a high degree of randomisation can end up bigger than the uncompressed original as a result!
If you really want to know about image compression, Google / Wiki is the place to start: but be warned that the math gets pretty hairy!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
Com Automation latest library for working with ms excel in mfc vosual studio vc++, can anyone suggest from where i get this.
|
|
|
|
|
|
- In VS menu Project -> Add New Item ... ->
- In "Add New Item" dialog select MFC in the treeview left, and then choose the "MFC Class From Typelib" and press the Add button
- In the "Add Class From Typelib" dialog choose the source of interface (registry or file), type libraries and then all the needed interfaces.
- then press OK
|
|
|
|
|
Is there any way in Windows to throttle a process' CPU usage?
If so, what's the technique?
EDITED:
I'm asking if one can programmatically throttle a process' CPU usage. Meaning managing the CPU time that a process is allowed to consume.
The difficult we do right away...
...the impossible takes slightly longer.
modified 19-May-23 22:07pm.
|
|
|
|
|
Not that I've ever seen.
Thinking about it, you might be able to kind of simulate it by limiting the cores the process can run on by setting processor affinity for it.
|
|
|
|
|
|
|
A tiny virtual machine.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
|
This looks awesome. Thanks, David.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I know this is the C/C++ forum, but here's an example of how to do this in C#. It was an interesting little research project.
The result can be seen in Task Manager quite easily. The CPU is limited to an AVERAGE of 20% in this example. It'll go as low as 8% and as high as 25% on my 13900K.
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace CsJobObjectSandbox
{
internal class Program
{
[StructLayout(LayoutKind.Explicit, Size = 8)]
public struct JobObject_CPU_Rate_Control_Information
{
[FieldOffset(0)]
public uint ControlFlags;
[FieldOffset(4)]
public uint CpuRate;
[FieldOffset(4)]
public uint Weight;
[FieldOffset(4)]
public ushort MinRate;
[FieldOffset(6)]
public ushort MaxRate;
}
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle;
}
public enum JobObject_Info_Class
{
BasicLimitInformation = 2,
BasicUiRestrictions = 4,
SecurityLimitInformation = 5,
EndOfJobItmeInformation = 6,
AssociateCompletionPortInformation = 7,
ExtendedLimitInformation = 9,
GroupInformation = 11,
NoticiationLimitInformation = 12,
GroupInformationEx = 14,
CpuRateControlInformation = 15,
NetRateControlinformation = 32,
NotificationLimitInformation = 33,
LimitViolationInformation2 = 34
}
private const uint JOBOBJECT_CPU_RATE_CONTROL_ENABLE = 0x1;
private const uint JOBOBJECT_CPU_RATE_CONTROL_WEIGHT_BASED = 0x2;
private const uint JOBOBJECT_CPU_RATE_CONTROL_HARD_CAP = 0x4;
private const uint JOBOBJECT_CPU_RATE_CONTROL_NOTIFY = 0x8;
private const uint JOBOBJECT_CPU_RATE_CONTROL_MIN_MAX_RATE = 0x10;
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern IntPtr CreateJobObject([In] ref SECURITY_ATTRIBUTES lpJobAttributes, string lpName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern bool SetInformationJobObject([In] IntPtr hJob, [In] JobObject_Info_Class jobObjectInfoClass, IntPtr lpJobObjectInfo, int cbJobObjectInfoLength);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern IntPtr AssignProcessToJobObject([In] IntPtr hJob, [In] IntPtr hprocess);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern bool CloseHandle([In] IntPtr hObject);
static void Main(string[] args)
{
SECURITY_ATTRIBUTES attributes = new();
attributes.nLength = Marshal.SizeOf(attributes);
attributes.lpSecurityDescriptor = IntPtr.Zero;
attributes.bInheritHandle = 0;
IntPtr jobHandle = CreateJobObject(ref attributes, "SandboxJobObject");
JobObject_CPU_Rate_Control_Information cpuLimitInfo = new()
{
ControlFlags = JOBOBJECT_CPU_RATE_CONTROL_ENABLE | JOBOBJECT_CPU_RATE_CONTROL_HARD_CAP,
CpuRate = 2000
};
int size = Marshal.SizeOf(typeof(JobObject_CPU_Rate_Control_Information));
IntPtr infoPointer = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(cpuLimitInfo, infoPointer, false);
bool result = SetInformationJobObject(jobHandle, JobObject_Info_Class.CpuRateControlInformation, infoPointer, size);
if (result)
{
Marshal.FreeHGlobal(infoPointer);
IntPtr processHandle = Process.GetCurrentProcess().Handle;
_ = AssignProcessToJobObject(jobHandle, processHandle);
Task task = TestMethodAsync();
task.Wait();
}
CloseHandle(jobHandle);
}
static Task TestMethodAsync()
{
return Task.Factory.StartNew(() =>
{
long y = 0;
Parallel.For((long)0, (long)10000000000, (x) =>
{
y = x++ * 2;
});
});
}
}
}
|
|
|
|
|
Just out of curiosity, if you set a hard cap of 20%, what causes it to exceed that?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
I don't know for sure, but I suspect thread scheduling on different cores running at different speeds.
On a 13900, you have 8 performance cores, which support HT, and 16 efficiency cores, which don't support HT. So I have 24 cores that can run at vastly different speeds, supporting 32 threads.
|
|
|
|
|
Nice. Learn something new every day.
|
|
|
|
|
I am revoking my license to this
modified 20-May-23 16:42pm.
|
|
|
|
|
This is what we refer to as a "code dump". Usually pointless except in the mind of the poster.
Taking credit for someone else's code it appears. "Signing" with some trivial changes.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|